在django项目当中,利用opencv对手势进行识别,并采用StreamingHttpResponse流式输出手势的状态,并将其放在 url = 'http://127.0.0.1:8000/finger_choose' 这个接口当中。
后续我想利用requests模块对其访问,代码如下:
import requests
from contextlib import closing
from collections import deque
url = 'http://127.0.0.1:8000/finger_choose'
res = requests.get(url, stream=True)
pts = deque(maxlen=2)
with closing(res) as r1:
for i in r1.iter_content():
pts.append(i)
if pts[0] == b'1' and pts[-1] == b'2':
print('握拳')
但是将requests模块嵌入到finger_url当中,就无法运行了,因为requests部分是一个死循环。
请问有方法进行改进吗?即我对url = 'http://127.0.0.1:8000/finger_choose'进行监测,如果是握拳,那么对前端的值进行一个修改。
def finger_url(request):
user_pose = {}
pts = deque(maxlen=2)
url = 'http://127.0.0.1:8000/finger_choose'
res = requests.get(url, stream=True)
if request.method == 'POST':
with closing(res) as r1:
for i in r1.iter_content():
pts.append(i)
if pts[0] == b'1' and pts[-1] == b'1':
user_pose['pose'] = '握拳'
return JsonResponse(user_pose)
return render(request,'selects.html',locals())