def count_1(): # cap = cv2.VideoCapture('http://192.168.1.104:8080/shot.jpg') 型号 = v4.get() reta = os.path.isfile(r"./imgs/" + 型号 + 'temp.jpg') if not reta: a = showinfo(title="样本确认", message="没有样本,请先登录!") cv2.destroyAllWindows() return() img1 = cv2.imread(r"./imgs/" + 型号 + 'temp.jpg',0) config = {'host':'127.0.0.1', 'user':'******', 'password':'******', 'port':'3306', 'database':'test', 'charset':'utf8' } try: conn = mysql.connector.Connect(**config) except mysql.connector.Error as e: print('connect fails!{}'.format(e)) cursor = conn.cursor() #查 try: sql = "SELECT * FROM prov WHERE XH = %s" cursor.execute(sql,(型号,)) sel_data = cursor.fetchone() print(sel_data) except mysql.connector.Error as e: print('query error!{}'.format(e)) finally: cursor.close() conn.close() cv2.namedWindow("image") try: w,h,threshold = sel_data[1],sel_data[2],sel_data[3] except: w = img1.shape[1] h = img1.shape[0] threshold = 69#0.78#0.80 #0.63 if w < img1.shape[1] or h < img1.shape[0]: w = img1.shape[1] h = img1.shape[0] threshold = 69#0.78#0.80 #0.63 cv2.createTrackbar('w','image',w,255,nothing) cv2.createTrackbar('h','image',h,255,nothing) cv2.createTrackbar('threshold','image',threshold,100,nothing) cap = cv2.VideoCapture(0) i=0 while(1): ret,image1 = cap.read() i += 1 if i == 20: break cap.release() while(1): w = cv2.getTrackbarPos('w','image') h = cv2.getTrackbarPos('h','image') threshold = cv2.getTrackbarPos('threshold','image')/100 if img1.shape[1] > 1.1*w or img1.shape[0] > 1.1*h: a = showinfo(title="w|h确认", message="w|h数值过小,请再试一次!") cv2.destroyAllWindows() break img = cv2.resize(image1,(800,600),interpolation = cv2.INTER_CUBIC) rectangles = [] counter = 1 scaleFactor =2#1.25 scale = 1 font = cv2.FONT_HERSHEY_PLAIN #寻找roi轮廓并作形状比较,<0.1 填充矩形 for resized in pyramid(img,scaleFactor): scale = float(img.shape[1]) / float(resized.shape[1]) for (x,y,roi) in sliding_window(resized,20,(w,h)): if roi.shape[1] != w or roi.shape[0] != h: #print("here") continue try: img2 = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY) ret = cv2.matchTemplate(img2,img1,cv2.TM_CCOEFF_NORMED) # 3.这边是Python/Numpy的知识,后面解释 loc = np.where(ret >= threshold) # 匹配程度大于%80的坐标y,x for pt in zip(*loc[::-1]): # *号表示可选参数 rx,ry,rx2,ry2 = int(x*scale),int(y*scale),int((x+w)*scale),int((y+h)*scale) rectangles.append([rx,ry,rx2,ry2,(ret[pt[0],pt[1]])]) except: pass counter += 1 windows = np.array(rectangles) boxes = nms(windows,1) if len(boxes) > 0: a = boxes.tolist() c = [] for i in a: if i not in c: c.append(i) a = c else: a = [] for (x,y,x2,y2,score) in a: cv2.rectangle(img,(int(x),int(y)),(int(x2),int(y2)),(0,0,255),1) cv2.putText(img,"%f"%score,(int(x),int(y)),font,1,(0,0,255)) v2.set(len(a)) cv2.imshow("image",img) q = cv2.waitKey(50) & 0xff if q == 13: # Enter #name = v5.get()+v4.get()+'.jpg' cv2.imwrite("./dinDan/temp.jpg",img) config = {'host':'127.0.0.1', 'user':'******', 'password':'******', 'port':'3306', 'database':'test', 'charset':'utf8' } 型号 = v4.get() try: conn = mysql.connector.Connect(**config) except mysql.connector.Error as e: print('connect fails!{}'.format(e)) cursor = conn.cursor() #改 try: sql = "Update prov Set XH = %s , w = %s ,h = %s ,threshold = %s Where XH = %s" cursor.execute(sql,(型号,w,h,int(threshold*100),型号)) except mysql.connector.Error as e: print('Update error!{}'.format(e)) finally: conn.commit() cursor.close() conn.close() break cv2.destroyAllWindows()
def count_1(): #global ret # cap = cv2.VideoCapture('http://192.168.1.104:8080/shot.jpg') 型号 = v4.get() reta = os.path.isfile(r"./imgs/" + 型号 + 'temp.jpg') if not reta: a = showinfo(title="样本确认", message="没有样本,请先登录!") cv2.destroyAllWindows() return () img1 = cv2.imread(r"./imgs/" + 型号 + 'temp.jpg', 0) pathfile = r'dataBase/test.mdb' tablename = r'prov' conn = mdb_conn(pathfile) cur = conn.cursor() #查 sql = "SELECT * FROM " + tablename + " where 型号 = '" + 型号 + "'" sel_data = mdb_sel(cur, sql) cv2.namedWindow("image") try: w, h, threshold = sel_data[0][1], sel_data[0][2], sel_data[0][3] except: w = img1.shape[1] h = img1.shape[0] threshold = 69 #0.78#0.80 #0.63 if w < img1.shape[1] or h < img1.shape[0]: w = img1.shape[1] h = img1.shape[0] threshold = 69 #0.78#0.80 #0.63 cv2.createTrackbar('w', 'image', w, 255, nothing) cv2.createTrackbar('h', 'image', h, 255, nothing) cv2.createTrackbar('threshold', 'image', threshold, 100, nothing) cap = cv2.VideoCapture(1) i = 0 while (1): ret, image1 = cap.read() i += 1 if i == 10: break cap.release() while (1): w = cv2.getTrackbarPos('w', 'image') h = cv2.getTrackbarPos('h', 'image') threshold = cv2.getTrackbarPos('threshold', 'image') / 100 if img1.shape[1] > 1.1 * w or img1.shape[0] > 1.1 * h: a = showinfo(title="w|h确认", message="w|h数值过小,请再试一次!") cv2.destroyAllWindows() break img = cv2.resize(image1, (800, 600), interpolation=cv2.INTER_CUBIC) rectangles = [] counter = 1 scaleFactor = 2 #1.25 scale = 1 font = cv2.FONT_HERSHEY_PLAIN #寻找roi轮廓并作形状比较,<0.1 填充矩形 for resized in pyramid(img, scaleFactor): scale = float(img.shape[1]) / float(resized.shape[1]) for (x, y, roi) in sliding_window(resized, 20, (w, h)): if roi.shape[1] != w or roi.shape[0] != h: #print("here") continue try: img2 = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) ret = cv2.matchTemplate(img2, img1, cv2.TM_CCOEFF_NORMED) # 3.这边是Python/Numpy的知识,后面解释 loc = np.where(ret >= threshold) # 匹配程度大于%80的坐标y,x for pt in zip(*loc[::-1]): # *号表示可选参数 rx, ry, rx2, ry2 = int(x * scale), int(y * scale), int( (x + w) * scale), int((y + h) * scale) rectangles.append( [rx, ry, rx2, ry2, (ret[pt[0], pt[1]])]) except: pass counter += 1 windows = np.array(rectangles) boxes = nms(windows, 1) if len(boxes) > 0: a = boxes.tolist() c = [] for i in a: if i not in c: c.append(i) a = c else: a = [] for (x, y, x2, y2, score) in a: cv2.rectangle(img, (int(x), int(y)), (int(x2), int(y2)), (0, 0, 255), 1) cv2.putText(img, "%f" % score, (int(x), int(y)), font, 1, (0, 0, 255)) v2.set(len(a)) cv2.imshow("image", img) q = cv2.waitKey(50) & 0xff if q == 13: # Enter #增 sql = "Insert Into " + tablename + " Values ('" + 型号 + "', " + str( w) + ", " + str(h) + ", " + str(threshold * 100) + ")" print(sql) if mdb_add(conn, cur, sql): print("插入成功!") else: print("插入失败!") #改 sql = "Update " + tablename + " Set 型号 = '" + 型号 + "', w = " + str( w) + ", threshold = " + str( threshold * 100) + ", h = " + str(h) + " where 型号 = '" + 型号 + "'" if mdb_modi(conn, cur, sql): print("修改成功!") else: print("修改失败!") cur.close() #关闭游标 conn.close() #关闭数据库连接 break cv2.destroyAllWindows()
while(1): w = cv2.getTrackbarPos('w','image') h = cv2.getTrackbarPos('h','image') #w,h = img1.shape[:2] img = cv2.imread(test_image) rectangles = [] counter = 1 scaleFactor =2#1.25 scale = 1 font = cv2.FONT_HERSHEY_PLAIN #寻找roi轮廓并作形状比较,<0.1 填充矩形 for resized in pyramid(img,scaleFactor): scale = float(img.shape[1]) / float(resized.shape[1]) for (x,y,roi) in sliding_window(resized,20,(w,h)): if roi.shape[1] != w or roi.shape[0] != h: continue try: ''' ''' img2 = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY) ret = cv2.matchTemplate(img2,img1,cv2.TM_CCOEFF_NORMED) ''' for score1 in ret: for score in score1: print(score)
img_path = "/home/dvaliu/Downloads/CarData/TestImages/test-10.pgm" svm, extractor = car_detector() detect = cv2.xfeatures2d.SIFT_create() w, h = 100, 40 img = cv2.imread(img_path) #img = cv2.imread(test_image) rectangles = [] counter = 1 scaleFactor = 1.25 scale = 1 font = cv2.FONT_HERSHEY_PLAIN for resized in pyramid(img, scaleFactor): scale = float(img.shape[1]) / float(resized.shape[1]) for (x, y, roi) in sliding_window(resized, 20, (100, 40)): if roi.shape[1] != w or roi.shape[0] != h: continue try: bf = bow_features(roi, extractor, detect) _, result = svm.predict(bf) a, res = svm.predict(bf, flags=cv2.ml.STAT_MODEL_RAW_OUTPUT | cv2.ml.STAT_MODEL_UPDATE_MODEL) print "Class: %d, Score: %f, a: %s" % (result[0][0], res[0][0], res) score = res[0][0] if result[0][0] == 1: if score < -1.0: rx, ry, rx2, ry2 = int(x * scale), int(y * scale), int((x+w) * scale), int((y+h) * scale) rectangles.append([rx, ry, rx2, ry2, abs(score)])