00001 from opencv.highgui import *
00002 from opencv.cv import *
00003 import numpy
00004 import scipy
00005 import sys
00006 import os
00007 from string import split
00008
00009 image = None
00010 image_clone = None
00011 in_draw = False
00012
00013 bb_list = []
00014
00015 def show_bb(image, bbs):
00016 pass
00017
00018
00019 def on_mouse(event, x, y, flags, p ):
00020 global start, image, image_clone, in_draw
00021
00022 if not image:
00023 return
00024
00025 cvCopy(image_clone,image)
00026
00027 if event == CV_EVENT_LBUTTONDOWN:
00028 start = (x,y)
00029 in_draw = True
00030 if event == CV_EVENT_MOUSEMOVE:
00031 if in_draw:
00032 start_x = min(start[0],x)
00033 start_y = min(start[1],y)
00034 end_x = max(start[0],x)
00035 end_y = max(start[1],y)
00036 cvRectangle(image, cvPoint(start_x,start_y), cvPoint(end_x,end_y),CV_RGB(255,0,0))
00037 elif event == CV_EVENT_LBUTTONUP:
00038 if in_draw:
00039 in_draw = False
00040 start_x = min(start[0],x)
00041 start_y = min(start[1],y)
00042 end_x = max(start[0],x)
00043 end_y = max(start[1],y)
00044 cvRectangle(image_clone, cvPoint(start_x,start_y), cvPoint(end_x,end_y),CV_RGB(255,0,0))
00045 cvRectangle(image, cvPoint(start_x,start_y), cvPoint(end_x,end_y),CV_RGB(255,0,0))
00046 bb_list.append((start_x,start_y,end_x,end_y))
00047
00048
00049 cvShowImage("image",image)
00050
00051
00052 def read_bb(bb_name):
00053 global bb_dict
00054 bb_file = open(bb_name,"r")
00055 for line in bb_file:
00056 items = split(line)
00057 bb_dict[items[0]] = map(int,items[1:])
00058 bb_file.close()
00059 return bb_dict
00060
00061 def annotate(image_name, bb_name):
00062 global image, image_clone, bb_list
00063
00064 image = cvLoadImage(image_name)
00065 image_clone = cvCloneImage(image)
00066
00067
00068 cvNamedWindow("image",1)
00069 cvShowImage("image",image)
00070 cvSetMouseCallback( "image", on_mouse, None );
00071
00072 while True:
00073 c = '%c' % (cvWaitKey(0) & 255)
00074
00075 if( c == '\x1b' or c=='q'):
00076 print "Exiting, not saving to file"
00077 break;
00078
00079 if c=='s':
00080 print "Exiting, saving to file: %s"%bb_name
00081 file = open(bb_name,"a")
00082 print >>file, image_name,
00083 for bb in bb_list:
00084 for x in bb:
00085 print >>file, x,
00086 print >>file
00087 file.close()
00088 break;
00089
00090
00091
00092 if __name__=="__main__":
00093 if len(sys.argv)<3:
00094 print "Usage: annotate.py bb_file image"
00095 sys.exit(1)
00096
00097 annotate(sys.argv[2], sys.argv[1])
00098
00099
00100