我正在使用python脚本进行面部验证。它在我的计算机上运行良好,并且在wamp服务器上运行良好。我已允许外部访问我的wamp服务器,因此我也可以通过移动设备访问本地主机。 当我在计算机上使用脚本时,它将触发网络摄像头并捕获我的图像并更新数据库(正确)
现在的问题是,每当我通过访问本地主机的IP地址在移动设备上运行相同的脚本时,它就会打开计算机网络摄像头而不是手机摄像头来捕获图像。
我希望脚本从我正在访问的任何设备上捕获图像。
链接获取haarcascade_frontalface_default.xml
此脚本是nazmiasri95在github上发布的代码的修改版,这里是original code
这是我的python代码
#!C:\Users\Gurminder\AppData\Local\Programs\Python\Python35-32\python.exe
print("Content-type: text/html\n\n");
# Import OpenCV2 for image processing
import cv2
import mysql.connector
import numpy as np
import os
import cgitb
cgitb.enable()
cnx = mysql.connector.connect(user='root', password='', host='localhost',
database='faceapi')
cursor = cnx.cursor()
def assure_path_exists(path):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.LBPHFaceRecognizer_create()
assure_path_exists("trainer/")
# Load the trained mode
recognizer.read('trainer/trainer.yml')
# Load prebuilt model for Frontal Face
cascadePath = "haarcascade_frontalface_default.xml"
# Create classifier from prebuilt model
faceCascade = cv2.CascadeClassifier(cascadePath);
# Set the font style
font = cv2.FONT_HERSHEY_SIMPLEX
# Initialize and start the video frame capture
cam = cv2.VideoCapture(0) # capturing from webcam (0) in args
# Loop
while True:
# Read the video frame
ret, im =cam.read()
# Convert the captured frame into grayscale
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
# Get all face from the video frame
faces = faceCascade.detectMultiScale(gray, 1.2,5)
# For each face in faces
for(x,y,w,h) in faces:
# Create rectangle around the face
cv2.rectangle(im, (x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
# Recognize the face belongs to which ID
Id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
# Check the ID if exist
if(Id == 1):
confi = round(100 - confidence, 1)
cursor.execute("insert into getmax (confident) values ('%s')" % (confi))
elif(Id ==2):
Id = "Swaran {0:.2f}%".format(round(100 - confidence, 2))
# Put text describe who is in the picture
cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
cv2.putText(im, str(confi), (x,y-40), font, 1, (255,255,255), 3)
# For gray window
cv2.rectangle(gray, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
cv2.putText(gray, str(confi), (x,y-40), font, 1, (255,255,255), 3)
cv2.imshow('gray',gray)
#cv2.imshow('Live Camera Capturing',im)
# If 'q' is pressed, close program
if cv2.waitKey(10) & 0xFF == ord('q'):
break
# Stop the camera
cam.release()
# Close all windows
cv2.destroyAllWindows()