import cv2 import requests import pickle import json import sys HOST = 'http://192.168.100.92:10016' url = HOST + '/query' cam_port = 0 cam = cv2.VideoCapture(cam_port) if not cam.isOpened(): print("Unable to read camera feed") sys.exit() imgid = 0 while True: print('====Select mode====\n1. Query mode(Defalut) \n2. Enroll mode\n3. Exit') print('> ', end='') mode = input() mode = 1 if not mode.isnumeric() else int(mode) if mode == 3: break elif mode == 2: print("what's your name?") iname = input() print('Picture key is "e"') while True: result, image = cam.read() cv2.imshow("GFG", image) if cv2.waitKey(5) & 0xFF == ord('e'): img = image.copy() res = requests.post(HOST + '/enroll', pickle.dumps({'img': image, 'name': iname})) print('enroll', res.text) if not res.text == 'fail': cv2.destroyWindow("GFG") break else: print('※ Query mode start, Exit key is "q" ※') while True: result, image = cam.read() img = image.copy() res = requests.post(url, pickle.dumps({'img': img, 'name': ''})) imgid += 1 if res.text == 'fail': cv2.putText(image, "fail", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow("GFG", img) else: info = json.loads(res.text) x1 = info['x1'] y1 = info['y1'] x2 = info['x2'] y2 = info['y2'] score = round(info['score'] * 100, 1) confi = info['file'] + ' ' + str(score) + '%' if info['file'] != 'Unknown' else info['file'] if result: cv2.putText(image, confi, (int(x2)+20, int(y2)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 3) cv2.imshow("GFG", image) else: break if cv2.waitKey(5) & 0xFF == ord('q'): break cv2.destroyWindow("GFG") cam.release()