本文共 7622 字,大约阅读时间需要 25 分钟。
import tensorflow as tffrom PIL import Image# im = Image.open('D:\\deng\\ppp\\888.png')# data = list(im.getdata())# result = [(255-x)*1.0/255.0 for x in data]# print(result)# xs = tf.placeholder(tf.float32, name='x_input')# x_image = tf.reshape(xs, [-1, 28, 28, 1])def prediction_num(result): with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.import_meta_graph('D:/deng/model2/model.ckpt.meta') saver.restore(sess, "D:/deng/model2/model.ckpt") # 这里使用了之前保存的模型参数 pred = tf.get_collection('network-output')[0] prediction = tf.argmax(pred, 1) graph = tf.get_default_graph() xs = graph.get_operation_by_name('x_input').outputs[0] keep_prob = graph.get_operation_by_name('keep_prob').outputs[0] #keep_prob = graph.get_operation_by_name('y_inout').outputs[0] predint = prediction.eval(feed_dict={ xs: [result], keep_prob: 1.0}, session=sess) print("recognize result: %d" % predint[0])
from scipy.spatial import distance as distfrom imutils import perspectivefrom imutils import contoursimport numpy as npimport imutilsimport cv2import tensorflow_testfrom PIL import Imagedef midpoint(ptA, ptB): return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)#cap = cv2.VideoCapture("http://192.168.1.1:8080/?action=stream")cap = cv2.VideoCapture(0)#HSV颜色空间的红色区间Redlower=np.array([0, 43, 46])Redupper=np.array([124, 255, 255])# Redlower=np.array([0,43,46])# Redupper=np.array([25,255,255])while(True): # Capture frame-by-frame ret, frame = cap.read() frame = imutils.resize(frame, width=600) # Our operations on the frame come here blurred = cv2.GaussianBlur(frame, (7, 7), 0) hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) #hsv = cv2.GaussianBlur(hsv, (7, 7), 0) # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges # edged = cv2.Canny(hsv, 50, 100) #inRange()函数用于图像颜色分割 edged = cv2.inRange(hsv,Redlower,Redupper) edged = cv2.dilate(edged, None, iterations=2) edged = cv2.erode(edged, None, iterations=2) #在OpenCV中,可以用findContours()函数从二值图像中查找轮廓。 #可以使用compare(),inrange(),threshold(),adaptivethreshold(),canny()等函数由灰度图或色彩图创建二进制图像。 #RETR_EXTERNAL表示只检测最外层轮廓。CHAIN_APPROX_SIMPLE表示压缩水平方向,垂直方向,对角线方向的元素,只保留 #该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息。 cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] #cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #cnts = imutils.grab_contours(cnts) # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable #(cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour orig = frame.copy() box = cv2.minAreaRect(c) #box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) #绘制轮廓drawContours()函数 #第二个参数表示所有输入轮廓,每一个轮廓存储为一个点向量,即point类型的vector表示 #第三个参数为负数,则绘制所有轮廓 #第四个参数为轮廓颜色 #第五个参数为轮廓线条的粗细度 cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box #print(box) (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw the midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if pixelsPerMetric is None: # pixelsPerMetric = dB / args["width"] pixelsPerMetric = dB / 20.5 # compute the size of the object dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric D = 921.43 / pixelsPerMetric # draw the object sizes on the image cv2.putText(orig, "{:.1f}mm".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "{:.1f}mm".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "{:.1f}mm".format(D), (int(trbrX + 20), int(trbrY+20)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) # show the output image cut_img = orig[(int(tl[1])-60): (int(bl[1])+60), (int(tl[0])-70): (int(tr[0])+70)] #print(cut_img) if len(cut_img): print(cut_img) print(len(cut_img)) resize_img = cv2.resize(cut_img, (28, 28)) # 调整图像尺寸为28*28 resize_img = cv2.cvtColor(resize_img, cv2.COLOR_RGB2GRAY) ret, thresh_img = cv2.threshold(resize_img, 127, 255, cv2.THRESH_BINARY) # 二值化 cv2.imwrite('D:/deng/ppp/888.png', thresh_img) cv2.imshow('result', thresh_img) else: print("未检测到数字") im = Image.open('D:/deng/ppp/888.png') #im = cv2.imread('D:\\deng\\ppp\\888.png') #im_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) data = list(im.getdata()) result = [(255 - x) * 1.0 / 255.0 for x in data] tensorflow_test.prediction_num(result) cv2.imshow("Image", orig) if cv2.waitKey(1) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows() break
tensorflow 1.13.1
转载地址:http://wpfef.baihongyu.com/