用Python+Opencv寫了一個簡單的標註系統
用滑鼠標記目標,記錄目標的左上方和右下角
然後有方向鍵跳到下一幀。
可以用來進行簡單的標註了。
希望對大家有協助,代碼如下:
'''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()