def demo(net, im_path, anchor_file, des_dir='demo'): # Load the demo image if not os.path.exists(im_path): print 'Image `{:s}` not found!'.format(im_path) return #im = cv2.imread(im_path) im = exif.load_exif_jpg(im_path) #wdth = 1280 #eight = 720 #ulti = img.shape[0]*img.shape[1]*1.0/(width*height) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, anchor_file) #print scores #print boxes #print len(scores) timer.toc() print ('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) # Visualize detections for each class cls = 'obj' dets = np.hstack((boxes, scores)).astype(np.float32, copy=False) dets = boxes_filter(dets, NMS_CONFIG['PRE_NMS_TOPN'], NMS_CONFIG['NMS_THRESH'], NMS_CONFIG['POST_NMS_TOPN'], CONF_THRESH ) print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) #vis_detections(im, cls, dets, thresh=CONF_THRESH) #print dets # save result images output_dir = os.path.join(cfg.ROOT_DIR, 'data', des_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) # image_name = os.path.splitext(os.path.basename(im_path))[0] # res_im_file = os.path.join(output_dir, 'res_'+image_name+'.jpg') image_name = '_'.join(im_path.split('/')[-3:]) res_im_file = os.path.join(output_dir, image_name) #save_detection_res(im, res_im_file, dets) return (dets)
def demo(net, im_path, anchor_file, des_dir='demo'): # Load the demo image if not os.path.exists(im_path): print 'Image `{:s}` not found!'.format(im_path) return #im = cv2.imread(im_path) im = exif.load_exif_jpg(im_path) #wdth = 1280 #eight = 720 #ulti = img.shape[0]*img.shape[1]*1.0/(width*height) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, anchor_file) #print scores #print boxes #print len(scores) timer.toc() print('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) # Visualize detections for each class cls = 'obj' dets = np.hstack((boxes, scores)).astype(np.float32, copy=False) dets = boxes_filter(dets, NMS_CONFIG['PRE_NMS_TOPN'], NMS_CONFIG['NMS_THRESH'], NMS_CONFIG['POST_NMS_TOPN'], CONF_THRESH) print 'All {} detections with p({} | box) >= {:.1f}'.format( cls, cls, CONF_THRESH) #vis_detections(im, cls, dets, thresh=CONF_THRESH) #print dets # save result images output_dir = os.path.join(cfg.ROOT_DIR, 'data', des_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) # image_name = os.path.splitext(os.path.basename(im_path))[0] # res_im_file = os.path.join(output_dir, 'res_'+image_name+'.jpg') image_name = '_'.join(im_path.split('/')[-3:]) res_im_file = os.path.join(output_dir, image_name) #save_detection_res(im, res_im_file, dets) return (dets)
def predict(self): #args = parse_args() prototxt = self.defining caffemodel = self.net anchors = self.anchors #print('Called with args:') #print(args) cfg_from_file(self.cfg) print('Using config:') pprint.pprint(cfg) while not os.path.exists(caffemodel): print('Waiting for {} to exist...'.format(caffemodel)) time.sleep(10) caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(prototxt, caffemodel, caffe.TEST) net.name = os.path.splitext(os.path.basename(caffemodel))[0] print '\n\nLoaded network {:s}'.format(caffemodel) #img_list = ['tr.jpg'] #for img_name in img_list: # print '~' * 20 # im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', img_name) # print 'Demo for image: `{:s}`'.format(im_file) # demo(net, im_file, anchors) # plt.show() db = MongoClient('10.76.0.143', 27017)['plate'] db.authenticate('plateReader', 'IamReader') total_num = db.image.count() # total_num=1407 #bad=open('bad_list.txt','r') bad = open('id_path.txt', 'r') #bad=open('id_path.txt','r') #print total_num #idlist=range(total_num) #print idlist error1 = 0 f = open('re_predict_result_new.txt', 'w') for idlist in bad: #for i in bad: # num=int(i)-1 #print count #print num #print type(num) re_num = re.split(' ', idlist) #print re_num[0] num = int(re_num[0]) #print num temp = db.image.find_one({'_id': int(num)}) #temp=db.image.find_one({'_id',num+1}) if temp: #temp=db.image.find_one({'_id':num+1}) im_file = temp['path'] img = exif.load_exif_jpg(im_file) width = 1280 height = 720 multi = img.shape[0] * img.shape[1] * 1.0 / (width * height) multi = math.sqrt(multi) print multi resized_img = cv2.resize( img, (int(img.shape[1] / multi), int(img.shape[0] / multi))) resized_img_name = os.path.join('plate_buffer', 'resized_img.jpg') cv2.imwrite(resized_img_name, resized_img) re_img = cv2.imread(resized_img_name) #e_img_size = re_img.shape print im_file try: presicion = demo(net, resized_img_name, anchors) print presicion #print 'result' #print presicion #print int(presicion[0][2:3]) #print "a" #print len(temp['points']) #print "b" if len(temp['points']) > 0: f.write(str(num) + ' ') #f.write(str(temp['points'][0][0][0:1][0])+' '+str(temp['points'][0][0][1:2][0])+' ') #f.write(str(temp['points'][0][1][0:1][0])+' '+str(temp['points'][0][1][1:2][0])+' ') #f.write(str(temp['points'][0][2][0:1][0])+' '+str(temp['points'][0][2][1:2][0])+' ') #f.write(str(temp['points'][0][3][0:1][0])+' '+str(temp['points'][0][3][1:2][0])+' ') #print 'flag1' if len(presicion) != 0: f.write('*' + ' ') f.write( str(int(presicion[0][2:3] * multi + 1)) + ' ' + str(int(presicion[0][1:2] * multi - 1)) + ' ') f.write( str(int(presicion[0][2:3] * multi + 1)) + ' ' + str(int(presicion[0][3:4] * multi + 1)) + ' ') f.write( str(int(presicion[0][0:1] * multi - 1)) + ' ' + str(int(presicion[0][3:4] * multi + 1)) + ' ') f.write( str(int(presicion[0][0:1] * multi - 1)) + ' ' + str(int(presicion[0][1:2] * multi - 1))) #error1=error1+1 else: f.write('* ' + '0 ' + '0 ' + '0 ' + '0 ' + '0 ' + '0 ' + '0 ' + '0') f.write('\n') # print 'flags2' except: print 'we are wrong' pass #break f.close()
def predict(self): #args = parse_args() prototxt = self.defining caffemodel = self.net anchors = self.anchors #print('Called with args:') #print(args) cfg_from_file(self.cfg) print('Using config:') pprint.pprint(cfg) while not os.path.exists(caffemodel): print('Waiting for {} to exist...'.format(caffemodel)) time.sleep(10) caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(prototxt, caffemodel, caffe.TEST) net.name = os.path.splitext(os.path.basename(caffemodel))[0] print '\n\nLoaded network {:s}'.format(caffemodel) #img_list = ['tr.jpg'] #for img_name in img_list: # print '~' * 20 # im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', img_name) # print 'Demo for image: `{:s}`'.format(im_file) # demo(net, im_file, anchors) # plt.show() db = MongoClient('10.76.0.143', 27017)['plate'] db.authenticate('plateReader', 'IamReader') total_num=db.image.count() # total_num=1407 #bad=open('bad_list.txt','r') bad=open('id_path.txt','r') #bad=open('id_path.txt','r') #print total_num #idlist=range(total_num) #print idlist error1=0 f=open('re_predict_result_new.txt','w') for idlist in bad: #for i in bad: # num=int(i)-1 #print count #print num #print type(num) re_num=re.split(' ',idlist) #print re_num[0] num=int(re_num[0]) #print num temp=db.image.find_one({'_id':int(num)}) #temp=db.image.find_one({'_id',num+1}) if temp: #temp=db.image.find_one({'_id':num+1}) im_file=temp['path'] img = exif.load_exif_jpg(im_file) width = 1280 height = 720 multi = img.shape[0]*img.shape[1]*1.0/(width*height) multi = math.sqrt(multi) print multi resized_img = cv2.resize(img,(int(img.shape[1]/multi), int(img.shape[0]/multi))) resized_img_name = os.path.join('plate_buffer', 'resized_img.jpg') cv2.imwrite(resized_img_name, resized_img) re_img = cv2.imread(resized_img_name) #e_img_size = re_img.shape print im_file try: presicion= demo(net,resized_img_name,anchors) print presicion #print 'result' #print presicion #print int(presicion[0][2:3]) #print "a" #print len(temp['points']) #print "b" if len(temp['points'])>0: f.write(str(num)+' ') #f.write(str(temp['points'][0][0][0:1][0])+' '+str(temp['points'][0][0][1:2][0])+' ') #f.write(str(temp['points'][0][1][0:1][0])+' '+str(temp['points'][0][1][1:2][0])+' ') #f.write(str(temp['points'][0][2][0:1][0])+' '+str(temp['points'][0][2][1:2][0])+' ') #f.write(str(temp['points'][0][3][0:1][0])+' '+str(temp['points'][0][3][1:2][0])+' ') #print 'flag1' if len(presicion)!=0: f.write('*'+' ') f.write(str(int(presicion[0][2:3]*multi+1))+' '+str(int(presicion[0][1:2]*multi-1))+' ') f.write(str(int(presicion[0][2:3]*multi+1))+' '+str(int(presicion[0][3:4]*multi+1))+' ') f.write(str(int(presicion[0][0:1]*multi-1))+' '+str(int(presicion[0][3:4]*multi+1))+' ') f.write(str(int(presicion[0][0:1]*multi-1))+' '+str(int(presicion[0][1:2]*multi-1))) #error1=error1+1 else: f.write('* '+'0 '+'0 '+'0 '+'0 '+'0 '+'0 '+'0 '+'0') f.write('\n') # print 'flags2' except: print 'we are wrong' pass #break f.close()