I have developed a WebApplication in Django that has a view method which contains the OpevCV code that when triggered opens the User Webcam to detect its face. This app works fine in my localserver but when I have hosted it on PythonAnywhere it says camera not found as my PA hosting doesnt serve a camera.
So someone suggested me to open the webcam through javascript as it deals with the client machine and then pass its feed to server machine which is my hosting.
But as i am a rookie in Python i am not able to figure how to perform the above task.
I found this piece of js code but i dont know how and where to add this in my Django App.
Code for getting the feed with Javascript
var video = document.querySelector("#videoElement");
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({video: true}).then(function(stream) {
video.srcObject = stream;
}).catch(function(err0r) {
console.log("Something went wrong!");
});
}
My Python code for opening the camera and detecting faces is as follows (it works in localserver)
import cv2
cascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
frame = cv2.flip(frame, 1)
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=3)
for (x, y, w, h) in faces:
cropped = cv2.resize(frame[y:y+h, x:x+w], (198,198))
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
cv2.imshow('Stream', frame)
Any help is appreciated. Thank you in advance