1 Replies - 4724 Views - Last Post: 22 November 2012 - 12:32 PM Rate Topic: -----

#1 JavaLilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-September 12

Python/Opencv image recognition problems

Posted 22 November 2012 - 08:03 AM

I want to recognize gestures (waving, pointing, jumping etc). I thought I could do it by measuring how many pixels moved from frame to frame. Then I could write if this many pixels change then I most likely have this image. It seemed easier to write in java, but I couldn't seem to get it right with open cv. So,now I am trying to fix it with template matching, but I keep getting a NameError: global name 'LoadImage' is not defined error. With template matching, I thought I could draw contours around movement and then match the template from the movement, but i'm having trouble zoning in on just hand or just head movement.

Also, I wrote in an AbsDiff function, but if I run it (without the attempted template matching code, it turns the image black and reflects the text onto the bottom half of the screen). If I try to run the code without the template matching code I get UnboundLocalError: local variable 'contour' referenced before assignment.

Are there cv (2.4) functions that can make this process easier? Could someone help me pinpoint where these errors are coming from? I looked at open cv 2.4 docs and played around with other image manipulation code but I started getting confused with all of the image bit parameter requirements.

import cv
import cv2

class Capture:

    #similar to Java constructor
    def __init__(self):
        self.capture = cv.CaptureFromCAM(0)

    def run(self):
        #"Grab" your first frame
        # cpic = the current frame that you are working with
        cpic = cv.QueryFrame(self.capture)
        cpic_size = cv.GetSize(cpic)
        track = True
        #current color image
        curr_image =  cv.CreateImage(cv.GetSize(cpic), 8, 1)
        motion_image = cv.CreateImage(cv.GetSize(cpic), cv.IPL_DEPTH_32F, 3)
        grey_image = cv.CreateImage(cv.GetSize(cpic), cv.IPL_DEPTH_8U, 1)
        storage = cv.CreateMemStorage(0)
        while True:
           curr_image = cv.QueryFrame(self.capture)
           if track:
                frame_diff = cv.CloneImage(curr_image)
                temp = cv.CloneImage(curr_image)
                cv.ConvertScale(curr_image, motion_image, 1.0, 0.0)
                first = False
                cv.RunningAvg(curr_image, motion_image, 0.020, None)
           # Convert the scale of the moving average.
           cv.ConvertScale(frame_diff, temp, 1.0, 0.0)

           # Minus the current frame from the moving average.
           #cv.AbsDiff(curr_image, temp, frame_diff)
           threshold = 100

           image = LoadImage('C:\Users\___.jpeg')  
           template = grey_image

           W,H = GetSize(image)
           w,h = GetSize(grey_image)

           width = W-w+1
           height = H-h+1

           result = CreateImage ((width,height),32,1)
           MatchTemplate (image,template,result,CV_TM_CCORR)

           (min_x,min_y,minloc,maxloc) = MinMaxLoc(result)
           (x,y) = minloc

           THRESHOLD = 10;
           if minloc < THRESHOLD:
               contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)

           print result


           #points = [] 
           while contour:
               #bound_rect = cv.BoundingRect(list(contour))
               contour = contour.h_next()

               scalarMult = 10
               x = 100
               y = 100
               w = 100
               h = 100
               point1 = (x,y)
               point2 = (x+w,x+y)

           #Print the font on the screen 
           font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)
           #Show your resultant images
           threshold = 100
           cv.ShowImage ('CaptureLive',curr_image)
           cv.ShowImage ('Capture Light',grey_image)
           cv.ShowImage ('Capture Motion',motion_image)
           c = cv.WaitKey(7)%0x100
           if c == 27:

if __name__=="__main__":
    t = Capture()

Is This A Good Question/Topic? 0
  • +

Replies To: Python/Opencv image recognition problems

#2 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Python/Opencv image recognition problems

Posted 22 November 2012 - 12:32 PM

The first instance of contour in your program is here.

      if minloc < THRESHOLD:
069	               contour = cv.FindContours(grey_image, storage, 

if minloc >= THRESHOLD then contour doesn't get to exist. -> UnboundLocalError when you get to the line that contains
while contour: 

either set a default value for contour (eg contour = None) in the init method or at the top of the function,
or add an else clause to that if statement where you provide a suitable alternative to contour being the result of cv.FindCountours if minloc < threshold

Also, I see lots of cv functions that are not prefixed with cv.

LoadImage, GetSize, MatchTemplate, CreateImage, etc.

These are defined in the cv module (and you can see how it should be by looking at the first half of your code. You'll see cv.CaptureFromCAM, cv.CreateImage, etc). You'll get name errors when trying to call these functions because you're calling them as if you've done
 from cv import * 
def LoadImage():

This post has been edited by Python_4_President: 23 November 2012 - 01:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1