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()
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: {}, Score: {}, a: {}".format(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)]) except: pass counter += 1 windows = np.array(rectangles) boxes = nms(windows, 0.25) for (x, y, x2, y2, score) in boxes: print(x, y, x2, y2, score) cv2.rectangle(img, (int(x), int(y)), (int(x2), int(y2)), (0, 255, 0), 1) cv2.putText(img, "{}".format(score), (int(x), int(y)), font, 1, (0, 255, 0)) cv2.imshow("img", img) cv2.waitKey(0)
#print(loc[::-1]) #print(ret) 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]])]) #right_bottom = (pt[0] + w, pt[1] + h) #cv2.rectangle(img_rgb, pt, right_bottom, (0, 0, 255), 2) except: pass counter += 1 windows = np.array(rectangles) boxes = nms(windows,1) #boxes1 = np.stack(boxes,0) #print (boxes) a = boxes.tolist() #a = [[1, 2, 3], [1, 2, 3], [2, 3, 4], [2, 3, 4], [2, 3, 5], [5, 6, 7]] #a = [a[i] for i in range(len(a)) if a[i] not in a[:i]] #a = [[1, 2, 3], [1, 2, 3], [2, 3, 4], [2, 3, 4], [2, 3, 5], [5, 6, 7]] c = [] for i in a: if i not in c: c.append(i) a = c #print(a)
img = cv2.imread(test_img_path) gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) pos_rects = [] for resized in pyramid(gray_img): for x, y, roi in sliding_window(resized): descriptors = extract_bow_descriptors(roi) if descriptors is None: continue prediction = svm.predict(descriptors) if prediction[1][0][0] == 1.0: raw_prediction = svm.predict( descriptors, flags=cv2.ml.STAT_MODEL_RAW_OUTPUT) score = -raw_prediction[1][0][0] if score > SVM_SCORE_THRESHOLD: h, w = roi.shape scale = gray_img.shape[0] / float(resized.shape[0]) pos_rects.append([int(x * scale), int(y * scale), int((x+w) * scale), int((y+h) * scale), score]) nms(np.array(pos_rects), NMS_OVERLAP_THRESHOLD) for x0, y0, x1, y1, score in pos_rects: cv2.rectangle(img, (int(x0), int(y0)), (int(x1), int(y1)), (0, 255, 255), 2) text = '%.2f' % score cv2.putText(img, text, (x0, y0 - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) cv2.imshow(test_img_path, img) cv2.waitKey(0)
_, result = svm.predict(descriptors) a, res = svm.predict(descriptors, flags=cv2.ml.STAT_MODEL_RAW_OUTPUT | cv2.ml.STAT_MODEL_UPDATE_MODEL) print("Class: {}, Score: {}, a: {}".format(result[0][0], res[0][0], res)) score = res[0][0] scores.append(score) if result[0][0] == 1: if score < -0.35: # all scores less than ths=is value will be treated as a good prediction. 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)]) except: print("The prediction went wrong!") counter += 1 windows = np.array(rectangles) boxes = nms(windows, overlap_threshold=0.25) for (x, y, x2, y2, score) in boxes: print(x, y, x2, y2, score) cv2.rectangle(img, (int(x), int(y)), (int(x2), int(y2)), (0, 255, 0), 1) cv2.putText(img, "{:f}".format(score), (int(x), int(y)), fontFace=font, fontScale=1, color=(0, 255, 0)) cv2.imshow("img", img) cv2.waitKey(0) cv2.destroyAllWindows()
#print(thresh2) img2, contours2, hierachy = cv2.findContours( thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt2 = contours2[0] score = cv2.matchShapes(cnt1, cnt2, 1, 0.0) #print(score) if score <= 0.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, abs(score)]) except: pass counter += 1 windows = np.array(rectangles) boxes = nms(windows, 0.1) for (x, y, x2, y2, score) in boxes: print(x, y, x2, y2, score) 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)) cv2.imshow("img", img) cv2.imshow("thresh", thresh) cv2.imshow("thresh3", thresh3) cv2.waitKey(0)
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()
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)]) except: pass counter += 1 windows = np.array(rectangles) boxes = nms(windows, 0.25) for (x, y, x2, y2, score) in boxes: print x, y, x2, y2, score cv2.rectangle(img, (int(x),int(y)),(int(x2), int(y2)),(0, 255, 0), 1) cv2.putText(img, "%f" % score, (int(x),int(y)), font, 1, (0, 255, 0)) cv2.imshow("img", img) cv2.waitKey(0)