如何使用Python在Face API调用(Microsoft认知服务)中使用本地照片进行处理
更新: 我以前使用过如何使用Python在Face API调用(Microsoft认知服务)中使用本地照片进行处理,python,microsoft-cognitive,face-api,Python,Microsoft Cognitive,Face Api,更新: 我以前使用过face\u client.face.detect\u with\u url,效果很好。所以我的网络应该很好(我想可能吧)。但是,当我切换到face\u client.face.detect\u with\u stream时,它仍然收到错误。。。最后,我用reference重写了代码,它可以成功运行。我仍然不知道为什么会出现上一个问题,但非常感谢您的建议@Jim Xu,@Satya V 通过官方示例(Microsoft认知服务)后,通过python进行人脸检测。我尝试用局部
face\u client.face.detect\u with\u url
,效果很好。所以我的网络应该很好(我想可能吧)。但是,当我切换到face\u client.face.detect\u with\u stream
时,它仍然收到错误。。。最后,我用reference重写了代码,它可以成功运行。我仍然不知道为什么会出现上一个问题,但非常感谢您的建议@Jim Xu,@Satya V
通过官方示例(Microsoft认知服务)后,通过python进行人脸检测。我尝试用局部图像检测人脸。然而,我得到了一些错误 官方代码:
group_photo = 'test-image-person-group.jpg'
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)))
# Get test image
test_image_array = glob.glob(os.path.join(IMAGES_FOLDER, group_photo))
image = open(test_image_array[0], 'r+b')
# Detect faces
face_ids = []
faces = face_client.face.detect_with_stream(image)
参考官方代码,我的代码是:
IMAGES_FOLDER='' #the image path
test_image_array = glob.glob(os.path.join(IMAGES_FOLDER, '*jpg'))
image = open(test_image_array[0], 'r+b')
faces = face_client.face.detect_with_stream(image)
然而,也有一些错误
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
466
--> 467 low_conn.endheaders()
468
~/.conda/envs/Emotion_Azure/lib/python3.8/http/client.py in endheaders(self, message_body, encode_chunked)
1249 raise CannotSendHeader()
-> 1250 self._send_output(message_body, encode_chunked=encode_chunked)
1251
~/.conda/envs/Emotion_Azure/lib/python3.8/http/client.py in _send_output(self, message_body, encode_chunked)
1009 del self._buffer[:]
-> 1010 self.send(msg)
1011
~/.conda/envs/Emotion_Azure/lib/python3.8/http/client.py in send(self, data)
949 if self.auto_open:
--> 950 self.connect()
951 else:
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/urllib3/connection.py in connect(self)
361
--> 362 self.sock = ssl_wrap_socket(
363 sock=conn,
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/urllib3/util/ssl_.py in ssl_wrap_socket(sock, keyfile, certfile, cert_reqs, ca_certs, server_hostname, ssl_version, ciphers, ssl_context, ca_cert_dir, key_password, ca_cert_data)
396
--> 397 return context.wrap_socket(sock)
398
~/.conda/envs/Emotion_Azure/lib/python3.8/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
499 # ctx._wrap_socket()
--> 500 return self.sslsocket_class._create(
501 sock=sock,
~/.conda/envs/Emotion_Azure/lib/python3.8/ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session)
1039 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
-> 1040 self.do_handshake()
1041 except (OSError, ValueError):
~/.conda/envs/Emotion_Azure/lib/python3.8/ssl.py in do_handshake(self, block)
1308 self.settimeout(None)
-> 1309 self._sslobj.do_handshake()
1310 finally:
OSError: [Errno 0] Error
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/universal_http/requests.py in send(self, request, **kwargs)
138 try:
--> 139 response = session.request(
140 request.method,
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep, **send_kwargs)
531
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py in send(self, request, **kwargs)
642 # Send the request
--> 643 r = adapter.send(request, **kwargs)
644
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
497 except (ProtocolError, socket.error) as err:
--> 498 raise ConnectionError(err, request=request)
499
ConnectionError: [Errno 0] Error
During handling of the above exception, another exception occurred:
ClientRequestError Traceback (most recent call last)
<ipython-input-15-122d5fe49d09> in <module>
----> 1 faces = face_client.face.detect_with_stream(image)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/azure/cognitiveservices/vision/face/operations/_face_operations.py in detect_with_stream(self, image, return_face_id, return_face_landmarks, return_face_attributes, recognition_model, return_recognition_model, detection_model, custom_headers, raw, callback, **operation_config)
786 # Construct and send request
787 request = self._client.post(url, query_parameters, header_parameters, body_content)
--> 788 response = self._client.send(request, stream=False, **operation_config)
789
790 if response.status_code not in [200]:
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/service_client.py in send(self, request, headers, content, **kwargs)
334 kwargs.setdefault('stream', True)
335 try:
--> 336 pipeline_response = self.config.pipeline.run(request, **kwargs)
337 # There is too much thing that expects this method to return a "requests.Response"
338 # to break it in a compatible release.
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/__init__.py in run(self, request, **kwargs)
195 pipeline_request = Request(request, context) # type: Request[HTTPRequestType]
196 first_node = self._impl_policies[0] if self._impl_policies else self._sender
--> 197 return first_node.send(pipeline_request, **kwargs) # type: ignore
198
199 class HTTPSender(AbstractContextManager, ABC, Generic[HTTPRequestType, HTTPResponseType]):
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/__init__.py in send(self, request, **kwargs)
148 self._policy.on_request(request, **kwargs)
149 try:
--> 150 response = self.next.send(request, **kwargs)
151 except Exception:
152 if not self._policy.on_exception(request, **kwargs):
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/requests.py in send(self, request, **kwargs)
70 try:
71 try:
---> 72 return self.next.send(request, **kwargs)
73 except (oauth2.rfc6749.errors.InvalidGrantError,
74 oauth2.rfc6749.errors.TokenExpiredError) as err:
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/requests.py in send(self, request, **kwargs)
135
136 try:
--> 137 return self.next.send(request, **kwargs)
138 finally:
139 if old_max_redirects:
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/__init__.py in send(self, request, **kwargs)
148 self._policy.on_request(request, **kwargs)
149 try:
--> 150 response = self.next.send(request, **kwargs)
151 except Exception:
152 if not self._policy.on_exception(request, **kwargs):
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/pipeline/requests.py in send(self, request, **kwargs)
191 return Response(
192 request,
--> 193 self.driver.send(request.http_request, **kwargs)
194 )
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/universal_http/requests.py in send(self, request, **kwargs)
331 """
332 requests_kwargs = self._configure_send(request, **kwargs)
--> 333 return super(RequestsHTTPSender, self).send(request, **requests_kwargs)
334
335
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/universal_http/requests.py in send(self, request, **kwargs)
143 except requests.RequestException as err:
144 msg = "Error occurred in request."
--> 145 raise_with_traceback(ClientRequestError, msg, err)
146
147 return RequestsClientResponse(request, response)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/exceptions.py in raise_with_traceback(exception, message, *args, **kwargs)
49 error = exception(exc_msg, *args, **kwargs)
50 try:
---> 51 raise error.with_traceback(exc_traceback)
52 except AttributeError:
53 error.__traceback__ = exc_traceback
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/msrest/universal_http/requests.py in send(self, request, **kwargs)
137 session = kwargs.pop('session', self.session)
138 try:
--> 139 response = session.request(
140 request.method,
141 request.url,
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
528 }
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep, **send_kwargs)
531
532 return resp
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py in send(self, request, **kwargs)
641
642 # Send the request
--> 643 r = adapter.send(request, **kwargs)
644
645 # Total elapsed time of the request (approximately)
~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
496
497 except (ProtocolError, socket.error) as err:
--> 498 raise ConnectionError(err, request=request)
499
500 except MaxRetryError as e:
ClientRequestError: Error occurred in request., ConnectionError: [Errno 0] Error
---------------------------------------------------------------------------
OSError回溯(最近一次调用上次)
发送中的~/.conda/envs/Emotion\u Azure/lib/python3.8/site-packages/requests/adapters.py(self、request、stream、timeout、verify、cert、proxies)
466
-->467低位连接端头()
468
endheaders中的~/.conda/envs/Emotion\u Azure/lib/python3.8/http/client.py(self、message\u body、encode\u chunked)
1249升起CannotSendHeader()
->1250 self.\u send\u输出(消息体,encode\u chunked=encode\u chunked)
1251
~/.conda/envs/Emotion\u Azure/lib/python3.8/http/client.py in\u send\u输出(self、message\u body、encode\u chunked)
1009 del self.\u缓冲区[:]
->1010自动发送(msg)
1011
发送中的~/.conda/envs/Emotion\u Azure/lib/python3.8/http/client.py(self,data)
949如果self.auto\u打开:
-->950 self.connect()
951其他:
连接中的~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/urllib3/connection.py(self)
361
-->362 self.sock=ssl\u wrap\u套接字(
363 sock=接头,
ssl\u wrap\u套接字中的~/.conda/envs/Emotion\u Azure/lib/python3.8/site packages/urllib3/util/ssl\u.py(sock、密钥文件、证书文件、证书需求、证书、服务器主机名、ssl\u版本、密码、ssl\u上下文、证书目录、密钥密码、证书数据)
396
-->397返回上下文。包装_套接字(sock)
398
包装套接字中的~/.conda/envs/Emotion\u Azure/lib/python3.8/ssl.py(self、sock、服务器端、连接时握手、抑制不规则的EOF、服务器主机名、会话)
499#ctx._wrap_socket()
-->500返回self.sslsocket\u类。\u创建(
501袜子=袜子,
创建中的~/.conda/envs/Emotion\u Azure/lib/python3.8/ssl.py(cls、sock、服务器端、连接上的握手、抑制不规则的EOF、服务器主机名、上下文、会话)
1039 raise VALUE ERROR(“不应为非阻塞套接字指定连接上的do_握手”)
->1040自我握手
1041除了(OSError,VALUETERROR):
握手中的~/.conda/envs/Emotion\u Azure/lib/python3.8/ssl.py(self,block)
1308自我设置超时(无)
->1309自我介绍与他人握手
1310最后:
操作错误:[错误号0]错误
在处理上述异常期间,发生了另一个异常:
ConnectionError回溯(最近一次呼叫上次)
发送中的~/.conda/envs/Emotion\u Azure/lib/python3.8/site-packages/msrest/universal\u http/requests.py(self,request,**kwargs)
138尝试:
-->139响应=session.request(
140.请求方法,
请求中的~/.conda/envs/Emotion\u Azure/lib/python3.8/site-packages/requests/sessions.py(self、方法、url、参数、数据、标题、cookie、文件、身份验证、超时、允许重定向、代理、挂钩、流、验证、证书、json)
529发送文件更新(设置)
-->530 resp=自我发送(准备,**发送)
531
发送中的~/.conda/envs/Emotion_Azure/lib/python3.8/site-packages/requests/sessions.py(self,request,**kwargs)
642#发送请求
-->643 r=适配器.send(请求,**kwargs)
644
发送中的~/.conda/envs/Emotion\u Azure/lib/python3.8/site-packages/requests/adapters.py(self、request、stream、timeout、verify、cert、proxies)
497除了(ProtocolError,socket.error)作为错误:
-->498 raise CONNECTIONERR(错误,请求=请求)
499
ConnectionError:[Errno 0]错误
在处理上述异常期间,发生了另一个异常:
ClientRequestError回溯(最近一次呼叫上次)
在里面
---->1 faces=face\u客户端。face.detect\u与\u流(图像)
~/.conda/envs/Emotion\u Azure/lib/python3.8/site-packages/Azure/cognitiveservices/vision/face/operations//u face\u operations.py in detect\u with\u stream(self、image、return\u face\u id、return\u face\u landmarks、return\u face\u属性、识别模型、返回识别模型、检测模型、自定义头、原始、回调、**操作配置)
786#构造并发送请求
787 request=self.\u client.post(url、查询参数、标题参数、正文内容)
-->788 response=self.\u client.send(请求,流=False,**操作\u配置)
789
790如果response.status_代码不在[200]中:
发送中的~/.conda/envs/Emotion\u Azure/lib/python3.8/site-packages/msrest/service\u client.py(self、request、header、content、**kwargs)
334 kwargs.setdefault('stream',True)
335尝试:
-->336 pipeline_response=self.config.pipeline.run(请求,**kwargs)
337#有太多东西期望此方法返回“requests.Response”
338#在兼容版本中打破它。
~/.conda/envs/Emotion\u Azure/lib/python3.8/site packages/msrest/pipeline/\uuuuuuuu init\uuuuuuuuuuuu.py正在运行(self,request,**kwargs)
195管道_请求=请求(请求,上下文)#类型:请求[HTTPRequestType]
196第一个节点=self。如果self,则执行策略[0]_