Tracking tagging system (Python + opencv)

Source: Internet
Author: User

Using Python + opencv to write a simple tagging system
Mark the target with the mouse to record the upper left corner and lower right corner of the target
Then there is a direction key to jump to the next frame.
It can be used for simple tagging.
The Code is as follows:

'''Created on May 31, 2013@author: Yang'''# car.pyimport cvimport timeimport threadimport pythoncomimport pyHookimport osimport sysdef timer(no, interval):#     global x,y#     global move_x,move_y    pythoncom.PumpMessages()    def onKeyboardEvent(event):        global lins    key = event.Key    if key=='Up':        lins = 1    if key=='Down':        lins = 2    if key=='Left':        lins = 3    if key=='Right':        lins = 4    #print "Key:", event.Key#im=cv.CreateImage((400,400),cv.CV_8UC3,1)#    this is the method to define a mouse callback function. Several events are given in OpenCV documentationdef my_mouse_callback(event,x,y,flags,image):    global x1,y2,x2,y1    global flag    global box    global temp        try:        cv.Copy(image, temp)        #print type(box)        for j in xrange(len(box)):            xx1 = box[j][0]            yy1 = box[j][1]            xx2 = box[j][2]            yy2 = box[j][3]            cv.Rectangle(temp,(xx1,yy1),(xx2,yy2),(255,0,0),2,8)    except:        pass            if event==cv.CV_EVENT_LBUTTONDOWN:        # here event is left mouse button double-clicked        x1 = x        y1 = y        #print x,y        flag = 1        #cv.Rectangle(im,(x1,y1),(x,y),(255,0,0),2,8)    if event==cv.CV_EVENT_LBUTTONUP:        x2 = x        y2 = y        #print x1,y1,x2,y2        cv.Rectangle(temp,(x1,y1),(x2,y2),(255,0,0),2,8)        t = [x1,y1,x2,y2]        box.append(t)        flag = 0    #if event==cv.CV_EVENT_MOUSEMOVE:    if flag==1:        cv.Rectangle(temp,(x1,y1),(x,y),(255,0,0),2,8)def writebox(ff, box):    try:        for item in box:            t1 = item[0]            t2 = item[1]            t3 = item[2]            t4 = item[3]            ff.write(str(t1)+' '+str(t2)+' '+str(t3)+' '+str(t4)+' ')        ff.write('\n')    except:        pass    im = cv.LoadImage("frame_0000.jpg")cv.NamedWindow("Display",cv.CV_WINDOW_AUTOSIZE)x1 = 0y2 = 0x2 = 0y1 = 0flag = 0lins = 0box = []temp = cv.CreateImage(cv.GetSize(im), 8, 3)hm = pyHook.HookManager()hm.KeyDown = onKeyboardEventhm.HookKeyboard()#cv.SetMouseCallback("Display",my_mouse_callback,im)  thread.start_new_thread(timer,(1,1))  #binds the screen,function and image  path = 'F:/PETS2012/pets-new/S2_L1/Crowd_PETS09/S2/L1/Time_12-34/View_007'f = os.listdir(path)labout = open('view007.txt','w')count = 0for imagename in f:    imagepath = path+'/'+imagename     image = cv.LoadImage(imagepath)        cv.Copy(image,temp)    cv.SetMouseCallback("Display",my_mouse_callback,image)        while(1):        #cv.ShowImage("Display",im)        #cv.Rectangle(im,(80,88),(80+212,88+225),(0,0,0),-1)        #cv.ShowImage("Display",im)        #cv.ShowImage("Display",im)        cv.ShowImage("Display", temp)        k = cv.WaitKey(40)        if k=='f':            break        if lins==3:            lins=0            #temp = cv.CreateImage(cv.GetSize(im), 8, 3)            writebox(labout, box)            box=[]            break            print lins        if lins==4:            lins=0            #temp = cv.CreateImage(cv.GetSize(im), 8, 3)            labout.write(str(count)+' ')            writebox(labout, box)            labout.write('\n')            box=[]            break            print lins            count += 1    #if cv.WaitKey(15)%0x100==27:break        # waiting for clicking escape keycv.DestroyWindow("Display")labout.close()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.