Cannot open camera using OpenCV in VPS(Virtual Private Server) Hosting Centos 7 with Cyberpanel


This is the photo for the error when i am running the model
from php that running .py
i want open the camera and make face recogntion from website in laptop/pc and mobile devices

please help me
thanks

and this is the .py code:

import sys

import cv2
import numpy as np
import face_recognition
import os
from datetime import datetime

import pandas as pd

classNames = ['094009', '072803', '089117']
content = np.loadtxt('/home/absenpmo.site/public_html/build/file1.txt')
encodeListKnown = content
faces_name = [ ]
capture = cv2.VideoCapture(0) # argument 0 means return video from the first webcam on your computer

while len(faces_name) != 10: #get 10 Frames from Camera

    success, img = capture.read()
    #imgS = cv2.resize(img,(0,0),None,0.25,0.25)
    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

    facesCurFrame = face_recognition.face_locations(imgS)
    encodesCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)

    for encodeFace,faceLoc in zip(encodesCurFrame,facesCurFrame):
        matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
        faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
        matchIndex = np.argmin(faceDis)

        if matches[matchIndex]:
            name = classNames[matchIndex].upper()
            y1,x2,y2,x1 = faceLoc
            y1, x2, y2, x1 = y1*4,x2*4,y2*4,x1*4
            cv2.rectangle(img,(x1,y1),(x2,y2),(255,127,0),3)
            cv2.putText(img,name,(x1,y2+25),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,0),5)
            cv2.putText(img,name,(x1,y2+25),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
            #markAttendance(name)
            faces_name.append(name)
            cv2.imwrite('{}.jpg'.format(name), img)
capture.release()
faces_name =pd.DataFrame(faces_name)
faces_name['A'] = 1 
faces_name = faces_name.groupby([0]).agg('sum').reset_index()
faces_name=faces_name.sort_values(by=['A'], ascending=False)
id = faces_name.iloc[0][0]
faces_name=[]   
    # return id

print(id)

this is the updated full code:

import sys

import cv2
import numpy as np
import face_recognition
import os
from datetime import datetime

import pandas as pd

classNames = ['094009', '072803', '089117']
content = np.loadtxt('/home/absenpmo.site/public_html/build/file1.txt')
encodeListKnown = content
faces_name = []
capture = cv2.VideoCapture(0) # argument 0 means return video from the first webcam on your computer

while len(faces_name) != 10: #get 10 Frames from Camera

    success, img = capture.read()
    #imgS = cv2.resize(img,(0,0),None,0.25,0.25)
    imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)

    facesCurFrame = face_recognition.face_locations(imgS)
    encodesCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)

    for encodeFace,faceLoc in zip(encodesCurFrame,facesCurFrame):
        matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
        faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
        matchIndex = np.argmin(faceDis)

        if matches[matchIndex]:
            name = classNames[matchIndex].upper()
            y1,x2,y2,x1 = faceLoc
            y1, x2, y2, x1 = y1*4,x2*4,y2*4,x1*4
            cv2.rectangle(img,(x1,y1),(x2,y2),(255,127,0),3)
            cv2.putText(img,name,(x1,y2+25),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,0),5)
            cv2.putText(img,name,(x1,y2+25),cv2.FONT_HERSHEY_COMPLEX,1,(255,255,255),2)
            #markAttendance(name)
            faces_name.append(name)
            cv2.imwrite('{}.jpg'.format(name), img)
capture.release()
faces_name =pd.DataFrame(faces_name)
faces_name['A'] = 1 
faces_name = faces_name.groupby([0]).agg('sum').reset_index()
faces_name=faces_name.sort_values(by=['A'], ascending=False)
id = faces_name.iloc[0][0]
faces_name=[ ]   


print(id)

does your code run somewhere in the cloud ?
if so, how do you expect it to connect to an (usb ?) webcam, plugged into your local box ?

Yes thata in the cloud in hosting online, and i want to run it and open the camera from mobile devices and pc/laptop

well, you cannot use cv2.VideoCapture for this, on a remote server, bad plan ;(

maybe you can lookup, how to serve html/js to access the clients webcam from the browser, and send back images to analyze to the server

Any other path that i can use python for face recognition?

Other path using python or using flask?

it’s still same problem (no camera there) …

either simply make your users upload images, or access webcam from browser, or let them run a local script using their own webcam.

just run that stuff on your local computer. I see no reason to run this on a remote server.

if you need to expose this to users, you can package it all up as a desktop app (pyinstaller) with no web components at all. simply use VideoCapture and imshow() and waitKey() and that’s it.

if you need this as a web app, you’ll have to figure out how to use WebRTC (best solution) to send browser-side webcam feed to the server. and you’ll have to deeply understand the python web server so you can properly receive those user streams and handle them and send results back. this is not trivial. don’t do this unless it is a hard requirement.

a less complex option could be to run OpenCV.js in the client’s browser. then the server sees no data at all, and it can’t perform the computations. all of that happens on the user’s computer.

thank you for the answer :smiley:

thank you for the answer :smiley: