准备:
下载安装 opencv-python ,numpy
stupg.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>摄像头</title>
</head>
<body>
<h1>Webcam Stream</h1>
<video id="video" width="640" height="480" autoplay></video>
<script type="text/javascript">
var video = document.getElementById('video');
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
})
.catch(function (err0r) {
console.log("Something went wrong!");
});
}
</script>
</body>
</html>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('webcam/', views.webcam_view, name='stupg'),
]
views.py
import cv2
def webcam_feed():
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
image = cv2.resize(frame, (640, 480))
_, buffer = cv2.imencode('.jpg', image)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
cap.release()
def webcam_view(request):
return StreamingHttpResponse(webcam_feed(), content_type="multipart/x-mixed-replace;boundary=frame")
亲测有效
本小白在网上找了好多资源都用不了,甚至一度想框架嵌框架,发现嵌入框架还是不行,无奈去找了写代码的,80!!!,学生党几天的饭钱,果断放弃,柳暗花明又一村啊,看到chitgpt,无聊去试试,居然真的可以!!!真的很棒!!!,以上代码是chitgpt给出来的