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

**URL:** <https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693>\
**Category:** Python\
**Tags:** programming, flask\
**Created:** [January 19, 2023, 6:55am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693 "2023-01-19T06:55:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_P](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/christian_p/32/7000_2.png) [@Christian\_P](https://forum.opencv.org/u/Christian_P)\
**Post date:** [January 19, 2023, 6:55am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/1 "2023-01-19T06:55:42Z")

</div>

![CV2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/1881fb963cc565aae292bd93fb7425fe85913e81.png)  
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:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Christian\_P](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/christian_p/32/7000_2.png) [@Christian\_P](https://forum.opencv.org/u/Christian_P)\
**Post date:** [January 19, 2023, 6:57am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/2 "2023-01-19T06:57:55Z")

</div>

this is the updated full code:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 19, 2023, 7:30am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/3 "2023-01-19T07:30:24Z")

</div>

> [@Christian\_P](#):
>
> capture = cv2.VideoCapture(0) # argument 0 means return video from the first webcam _ **on your computer** _

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 ?

---

<div class="post-metadata">

**Author:** ![Christian\_P](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/christian_p/32/7000_2.png) [@Christian\_P](https://forum.opencv.org/u/Christian_P)\
**Post date:** [January 19, 2023, 11:23am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/4 "2023-01-19T11:23:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 19, 2023, 11:36am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/5 "2023-01-19T11:36:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christian\_P](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/christian_p/32/7000_2.png) [@Christian\_P](https://forum.opencv.org/u/Christian_P)\
**Post date:** [January 19, 2023, 11:43am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/6 "2023-01-19T11:43:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Christian\_P](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/christian_p/32/7000_2.png) [@Christian\_P](https://forum.opencv.org/u/Christian_P)\
**Post date:** [January 19, 2023, 11:46am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/7 "2023-01-19T11:46:28Z")

</div>

Other path using python or using flask?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [January 19, 2023, 11:50am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/8 "2023-01-19T11:50:51Z")

</div>

> [@Christian\_P](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 19, 2023, 12:52pm UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/9 "2023-01-19T12:52:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christian\_P](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/christian_p/32/7000_2.png) [@Christian\_P](https://forum.opencv.org/u/Christian_P)\
**Post date:** [January 20, 2023, 2:06am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/10 "2023-01-20T02:06:45Z")

</div>

> [@crackwitz](#):
>
> WebRTC

thank you for the answer 😃

---

<div class="post-metadata">

**Author:** ![Christian\_P](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/christian_p/32/7000_2.png) [@Christian\_P](https://forum.opencv.org/u/Christian_P)\
**Post date:** [January 20, 2023, 2:08am UTC](https://forum.opencv.org/t/cannot-open-camera-using-opencv-in-vps-virtual-private-server-hosting-centos-7-with-cyberpanel/11693/11 "2023-01-20T02:08:02Z")

</div>

thank you for the answer 😃
