def detect(net,meta,cam_param,im,isshow=False,issave=False,thresh=.3, hier_thresh=.5, nms=.45, name=None, output_dir=None): print "detection =============================" start_t = time.time() r = dn.detect_np(net,meta,im) bdict = {} bdict = allposition(r,cam_param) end_t = time.time() print name, "detection result:",bdict if isshow or issave : image = im.copy() for i in r: # print i sx1,sy1,sx2,sy2=i[2] sx1=sx1-sx2/2 sy1=sy1-sy2/2 cv2.rectangle(image,(int(sx1),int(sy1)),(int(sx1+sx2),int(sy1+sy2)),(0,255,0),3) if (sy1 > 10): cv2.putText(image, i[0]+"-"+str(round(i[1],2)), (int(sx1),int(sy1-6)), cv2.FONT_HERSHEY_COMPLEX_SMALL,0.8, (0, 255, 0) ) else: cv2.putText(image, i[0]+"-"+str(round(i[1],2)), (int(sx1),int(sy1+15)), cv2.FONT_HERSHEY_COMPLEX_SMALL,0.8, (0, 255, 0) ) if isshow: cv2.imshow("pre",image) cv2.waitKey(1) # cv2.imwrite("current.jpg",image) if issave: if name is None: name = 'test' if output_dir is None: output_dir = "./run_pic" t = str(end_t-start_t) cv2.imwrite(os.path.join(output_dir,"pre-"+name+"-"+t+".jpg"),image) return bdict
def detecttion(image, flag=True, hsvonly=False, save=False): if not flag: return None global net global meta global cam_param start_t = time.time() im = cv2.imread(image) # ballCenter, isClose = doSomething(im) # if hsvonly == True: # return ballCenter,isClose # if isClose: # return ballCenter,isClose r = dn.detect(net, meta, image, thresh=0.4) print r end_t = time.time() cost_t = round(end_t - start_t) # blist = [] # for i in r: # if i[0] == 'football': # blist = ballpostion(i,cam_param) bdict = {} bdict = allposition(r, cam_param) if save: for i in r: #print i sx1, sy1, sx2, sy2 = i[2] sx1 = sx1 - sx2 / 2 sy1 = sy1 - sy2 / 2 cv2.rectangle(im, (int(sx1), int(sy1)), (int(sx1 + sx2), int(sy1 + sy2)), (0, 255, 0), 3) if (sy1 > 10): cv2.putText(im, i[0], (int(sx1), int(sy1 - 6)), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.8, (0, 255, 0)) else: cv2.putText(im, i[0], (int(sx1), int(sy1 + 15)), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.8, (0, 255, 0)) # cv2.imwrite(image+"_pre_"+str(cost_t),im) cv2.imshow(filename, im) #cv2.waitKey(5000) #print r if len(bdict) < 1: return None # return blist,False return bdict, False