# otherwise load video else: camera = cv2.VideoCapture(args["video"]) # keep looping over frames while True: # grab frame (grabbed, frame) = camera.read() # check for last frame if not grabbed: break # detect barcode box = simple_barcode_detection.detect(frame) # draw bounding box cv2.drawContours(frame, box, -1, (0, 255, 0), 3) # show frame, record if key pressed cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if q, then quit if key == ord("q"): break # cleanup and close windows camera.release() cv2.destroyAllWindows()
time.sleep(2.0) else: vs = cv2.VideoCapture(args["video"]) while True: frame = vs.read() frame = frame[1] if args.get("video", False) else frame if frame is None: break # detect the barcode in the image box = detect(frame) #croped=croping(frame,box) #frame=read_barcodes(box) if box is not None: cv2.drawContours(frame, [box], -1, (0, 255, 0), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break
camera = cv2.VideoCapture(0) # otherwise, load the video else: camera = cv2.VideoCapture(args["video"]) # keep looping over the frames while True: # grab the current frame (grabbed, frame) = camera.read() # check to see if we have reached the end of the video if not grabbed: break # detect the barcode in the image box = simple_barcode_detection.detect(frame) # if a barcode was found, draw a bounding box on the frame cv2.drawContours(frame, [box], -1, (0,255,0), 2) # show the frame and record if the user presses a key cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the 'q' key is pressed, stop the loop if key == ord("q"): break # cleanup the camera and close any open windows camera.release() cv2.destroyAllWindows()
def train(do_train): # with tf.name_scope("input"): x = tf.placeholder(tf.float32, [None, 100, 100], name="x_input") _y = tf.placeholder(tf.float32, [None, 1], name="y_input") _x = tf.placeholder(tf.float32, [None, 1], name="predtion") _pred = _CNN(x)['output'] # 计算最终输出与标准之间的loss # 把均方误差也加入到集合里 # tf.add_to_collection("losses", tf.reduce_mean(tf.square(_pred - _y))) # cost = tf.add_n(tf.get_collection("losses"),name="loss") cost = tf.reduce_mean(tf.square(_pred - _y)) #学习率呈指数下降 # learning_rate = tf.train.exponential_decay(learning_rate=0.1,decay_rate=0.9,global_step = tf.Variable(0),decay_steps=10) #随机梯度下降算法 # optm = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) #Adam算法 optm = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost) _corr = tf.equal(_x, _y) accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) sess = tf.Session() sess.run(tf.global_variables_initializer()) # 训练次数 train_epochs = 100 # 保存model saver = tf.train.Saver(max_to_keep=1) # # P处理大小 # batch_size = 50 if do_train == 0: loss = 1 for epoch in range(train_epochs): x_data, y_data = init_data(epoch, 35) batch_x = x_data batch_y = y_data.reshape(len(y_data), 1) sess.run(optm, feed_dict={x: batch_x, _y: batch_y}) avg_cost = sess.run(cost, feed_dict={x: batch_x, _y: batch_y}) # print(pred) print("Epoch %03d/%03d" % (epoch, train_epochs)) print("Cost:" + str(avg_cost)) if avg_cost < loss: now = time.strftime('%Y-%m-%d-%H') loss = avg_cost saver.save( sess, '/home/zx/qrcode/model/model-' + now + '-' + str(epoch)) if do_train == 1: saver.restore(sess, '/home/zx/qrcode/model/model-2018-04-19-18-64') print("Read Model Ready!") train_acc = 0 test_epochs = 3 #p处理张数 pnum = 50 for i in range(test_epochs): x_data, y_data = test_data(i, pnum) y_data = y_data.reshape(len(y_data), 1) prediction = sess.run(_pred, feed_dict={x: x_data}) for i in range(len(prediction)): if prediction[i, ] > 0.5: prediction[i, ] = 1 else: prediction[i, ] = 0 train_acc = train_acc + sess.run(accr, feed_dict={ _x: prediction, _y: y_data }) train_acc = train_acc / test_epochs print("Accuracy: " + str(train_acc)) if do_train == 2: saver.restore(sess, '/home/zx/qrcode/model/model-2018-04-19-18-64') print("Read Model Ready!") img = cv2.imread("/home/zx/qrcode/data/_test/no2.jpg") # img = cv2.imread("/home/zx/qrcode/data/_test/no3.jpg", cv2.IMREAD_GRAYSCALE) if img is not None: img = cv2.resize(img, (399, 260)) cropImg = detect(img) # cv2.imshow("cropImg",cropImg) # cv2.waitKey(0) cropImg = cv2.resize(cropImg, (100, 100)) cv2.imwrite("/home/zx/qrcode/data/_test/demo/0.jpg", cropImg) cropImg = cropImg.reshape(1, 100, 100) // 255 print("Load Image Ready!") prediction = sess.run(_pred, feed_dict={x: cropImg}) print(prediction[0, ][0]) if prediction[0, ] > 0.5: print("是") else: print("不是")