问题描述
我正在开发通过从树莓派相机接收图像到计算机来提供Django中的人脸识别自动门服务。 当我单击下面图片中的按钮时,我想捕获显示在Django中的屏幕,但是我不知道如何编写view.py。帮帮我...
这是picamera代码
import socket
import cv2,os,urllib.request
import numpy as np
from django.conf import settings
#from django.apps import AppConfig
face_detector = cv2.CascadeClassifier(r'C:\Users\Desktop\University\last\django\face_project\picamera\haarcascade_frontalface_default.xml')
global frame,gray
#socket에서 수신한 버퍼를 반환하는 함수
def recvall(sock,count):
# 바이트 문자열
buf = b''
while count:
newbuf = sock.recv(count)
if not newbuf: return None
buf += newbuf
count -= len(newbuf)
return buf
class streaming_pi_camera(object):
def __init__(self):
HOST='my_ip'
PORT=8485
#TCP 사용
global s
s=socket.socket(socket.AF_INET,socket.soCK_STREAM)
#서버의 아이피와 포트번호 지정
s.bind((HOST,PORT))
# 클라이언트의 접속을 기다린다. (클라이언트 연결을 10개까지 받는다)
s.listen(10)
print('Socket Ready')
#연결,conn에는 소켓 객체,addr은 소켓에 바인드 된 주소
global conn
conn,addr=s.accept()
def __del__(self):
conn.close()
s.close()
cv2.destroyAllWindows()
def get_frame(self):
while True:
# client에서 받은 stringData의 크기 (==(str(len(stringData))).encode().ljust(16))
length = recvall(conn,16)
stringData = recvall(conn,int(length))
#data = np.fromstring(stringData,dtype = 'uint8')
frame = cv2.imdecode(np.fromstring(stringData,dtype=np.uint8),cv2.IMREAD_COLOR)
#face detector and label
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces_detected = face_detector.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=5)
for (x,y,w,h) in faces_detected:
cv2.rectangle(frame,pt1=(x,y),pt2=(x + w,y + h),color=(255,0),thickness=2)
global jpeg
ret,jpeg = cv2.imencode('.jpg',frame)
return jpeg.tobytes()
def get_image(self):
# 각 사용자 얼굴을 face_id 를 부여하여 얼굴 데이터를 받아들인다.
face_id = input('\n enter user id end press <return> ==> ')
#count 사진데이터 샘플 수
count = 0
while(True):
#gray = cv2.cvtColor(jpeg,cv2.COLOR_BGR2GRAY)
#faces = face_detector.detectMultiScale(gray,1.3,5)
for (x,h) in faces:
cv2.rectangle(frame,(x,(x+w,y+h),(255,2)
count += 1
# dataset 폴더에 사진을 저장
cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg",gray[y:y+h,x:x+w])
if count >= 30:
break
这是view.py
from django.shortcuts import render
from django.http.response import StreamingHttpResponse
from picamera.picamera import streaming_pi_camera
global frame
def index(request):
return render(request,'picamera/home.html')
# Web Page에 영상을 띄우는 Action
def convert_video(picamera):
while True:
frame = picamera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
def video_pi_camera(request):
return StreamingHttpResponse(convert_video(streaming_pi_camera()),content_type='multipart/x-mixed-replace; boundary=frame')
# Web Page 사용자 등록 버튼 Action
def register_user(request):
get_image()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)