client.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import cv2
  2. import requests
  3. import pickle
  4. import json
  5. import sys
  6. HOST = 'http://192.168.100.92:10016'
  7. url = HOST + '/query'
  8. cam_port = 0
  9. cam = cv2.VideoCapture(cam_port)
  10. if not cam.isOpened():
  11. print("Unable to read camera feed")
  12. sys.exit()
  13. imgid = 0
  14. while True:
  15. print('====Select mode====\n1. Query mode(Defalut) \n2. Enroll mode\n3. Exit')
  16. print('> ', end='')
  17. mode = input()
  18. mode = 1 if not mode.isnumeric() else int(mode)
  19. if mode == 3:
  20. break
  21. elif mode == 2:
  22. print("what's your name?")
  23. iname = input()
  24. print('Picture key is "e"')
  25. while True:
  26. result, image = cam.read()
  27. cv2.imshow("GFG", image)
  28. if cv2.waitKey(5) & 0xFF == ord('e'):
  29. img = image.copy()
  30. res = requests.post(HOST + '/enroll', pickle.dumps({'img': image, 'name': iname}))
  31. print('enroll', res.text)
  32. if not res.text == 'fail':
  33. cv2.destroyWindow("GFG")
  34. break
  35. else:
  36. print('※ Query mode start, Exit key is "q" ※')
  37. while True:
  38. result, image = cam.read()
  39. img = image.copy()
  40. res = requests.post(url, pickle.dumps({'img': img, 'name': ''}))
  41. imgid += 1
  42. if res.text == 'fail':
  43. cv2.putText(image, "fail", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
  44. cv2.imshow("GFG", img)
  45. else:
  46. info = json.loads(res.text)
  47. x1 = info['x1']
  48. y1 = info['y1']
  49. x2 = info['x2']
  50. y2 = info['y2']
  51. score = round(info['score'] * 100, 1)
  52. confi = info['file'] + ' ' + str(score) + '%' if info['file'] != 'Unknown' else info['file']
  53. if result:
  54. cv2.putText(image, confi, (int(x2)+20, int(y2)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
  55. cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 3)
  56. cv2.imshow("GFG", image)
  57. else:
  58. break
  59. if cv2.waitKey(5) & 0xFF == ord('q'):
  60. break
  61. cv2.destroyWindow("GFG")
  62. cam.release()