質問編集履歴

1

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

2020/05/28 07:45

投稿

colorful
colorful

スコア23

test CHANGED
File without changes
test CHANGED
@@ -95,3 +95,119 @@
95
95
  TypeError: Expected Ptr<cv::UMat> for argument '%s'
96
96
 
97
97
  ```
98
+
99
+
100
+
101
+ ```
102
+
103
+ [client.py]
104
+
105
+ import os
106
+
107
+ import json
108
+
109
+ import cv2
110
+
111
+ import base64
112
+
113
+ import numpy as np
114
+
115
+ from datetime import datetime
116
+
117
+ from flask import Flask, request, Response
118
+
119
+ app = Flask(__name__)
120
+
121
+ count = 0
122
+
123
+
124
+
125
+ image_dir = "./images"
126
+
127
+ if not os.path.isdir(image_dir):
128
+
129
+ os.mkdir(image_dir)
130
+
131
+
132
+
133
+ def detect_face(img):
134
+
135
+ face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
136
+
137
+ faces = face_cascade.detectMultiScale(img, 1.3, 5)
138
+
139
+ return faces
140
+
141
+
142
+
143
+ @app.route('/save', methods=['POST'])
144
+
145
+ def save_image():
146
+
147
+
148
+
149
+ data = request.data.decode('utf-8')
150
+
151
+ data_json = json.loads(data)
152
+
153
+ image = data_json['image']
154
+
155
+ image_dec = base64.b64decode(image)
156
+
157
+ data_np = np.fromstring(image_dec, dtype='uint8')
158
+
159
+ decimg = cv2.imdecode(data_np, 1)
160
+
161
+
162
+
163
+ gray_img = cv2.cvtColor(decimg, cv2.COLOR_BGR2GRAY)
164
+
165
+ faces = detect_face(gray_img)
166
+
167
+ for (x,y,w,h) in faces:
168
+
169
+ decimg = cv2.rectangle(decimg,(x,y),(x+w,y+h),(255,0,0),2)
170
+
171
+
172
+
173
+ global count
174
+
175
+ filename = "./images/image{}.png".format(count)
176
+
177
+ cv2.imwrite(filename, decimg)
178
+
179
+ count += 1
180
+
181
+
182
+
183
+ return Response(response=json.dumps({"message": "{} was saved".format(filename)}), status=200)
184
+
185
+
186
+
187
+ if __name__ == '__main__':
188
+
189
+ app.run(host='0.0.0.0', port=8080)
190
+
191
+ ```
192
+
193
+
194
+
195
+ ```
196
+
197
+ [実行結果]
198
+
199
+
200
+
201
+ * Serving Flask app "client" (lazy loading)
202
+
203
+ * Environment: production
204
+
205
+ WARNING: This is a development server. Do not use it in a production deployment.
206
+
207
+ Use a production WSGI server instead.
208
+
209
+ * Debug mode: off
210
+
211
+ * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
212
+
213
+ ```