现在的位置: 首页 > 综合 > 正文

Tracking标注系统(Python+Opencv)

2013年09月20日 ⁄ 综合 ⁄ 共 2507字 ⁄ 字号 评论关闭

用Python+Opencv写了一个简单的标注系统
用鼠标标记目标,记录目标的左上角和右下角
然后有方向键跳到下一帧。
可以用来进行简单的标注了。
希望对大家有帮助,代码如下:

'''
Created on May 31, 2013

@author: Yang
'''
# car.py
import cv
import time
import thread
import pythoncom
import pyHook
import os
import sys

def 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 documentation
def 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 = 0
y2 = 0
x2 = 0
y1 = 0
flag = 0
lins = 0
box = []
temp = cv.CreateImage(cv.GetSize(im), 8, 3)
hm = pyHook.HookManager()
hm.KeyDown = onKeyboardEvent
hm.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 = 0
for 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 key
cv.DestroyWindow("Display")
labout.close()

抱歉!评论已关闭.