teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

クライアント側のプログラムと実行結果を追加しました。

2020/05/28 07:45

投稿

colorful
colorful

スコア23

title CHANGED
File without changes
body CHANGED
@@ -46,4 +46,62 @@
46
46
  File "send.py", line 12, in send_image
47
47
  encimg = cv2.imencode(".png", img)
48
48
  TypeError: Expected Ptr<cv::UMat> for argument '%s'
49
+ ```
50
+
51
+ ```
52
+ [client.py]
53
+ import os
54
+ import json
55
+ import cv2
56
+ import base64
57
+ import numpy as np
58
+ from datetime import datetime
59
+ from flask import Flask, request, Response
60
+ app = Flask(__name__)
61
+ count = 0
62
+
63
+ image_dir = "./images"
64
+ if not os.path.isdir(image_dir):
65
+ os.mkdir(image_dir)
66
+
67
+ def detect_face(img):
68
+ face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
69
+ faces = face_cascade.detectMultiScale(img, 1.3, 5)
70
+ return faces
71
+
72
+ @app.route('/save', methods=['POST'])
73
+ def save_image():
74
+
75
+ data = request.data.decode('utf-8')
76
+ data_json = json.loads(data)
77
+ image = data_json['image']
78
+ image_dec = base64.b64decode(image)
79
+ data_np = np.fromstring(image_dec, dtype='uint8')
80
+ decimg = cv2.imdecode(data_np, 1)
81
+
82
+ gray_img = cv2.cvtColor(decimg, cv2.COLOR_BGR2GRAY)
83
+ faces = detect_face(gray_img)
84
+ for (x,y,w,h) in faces:
85
+ decimg = cv2.rectangle(decimg,(x,y),(x+w,y+h),(255,0,0),2)
86
+
87
+ global count
88
+ filename = "./images/image{}.png".format(count)
89
+ cv2.imwrite(filename, decimg)
90
+ count += 1
91
+
92
+ return Response(response=json.dumps({"message": "{} was saved".format(filename)}), status=200)
93
+
94
+ if __name__ == '__main__':
95
+ app.run(host='0.0.0.0', port=8080)
96
+ ```
97
+
98
+ ```
99
+ [実行結果]
100
+
101
+ * Serving Flask app "client" (lazy loading)
102
+ * Environment: production
103
+ WARNING: This is a development server. Do not use it in a production deployment.
104
+ Use a production WSGI server instead.
105
+ * Debug mode: off
106
+ * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
49
107
  ```