**已关闭。**此问题需要debugging details。目前不接受回答。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
8天前关闭。
Improve this question的
下面是我的代码:
def generate_dataset(nbr):
face_classifier = cv2.CascadeClassifier("resources/haarcascade_frontalface_default.xml")
mycursor.execute("select * from img_dataset WHERE img_person='" + str(nbr) + "'")
data1 = mycursor.fetchall()
for item in data1:
imagePath = "dataset/" + nbr + "." + str(item[0]) + ".jpg"
# print(imagePath)
try:
os.remove(imagePath)
except:
pass
mycursor.execute("delete from img_dataset WHERE img_person='" + str(nbr) + "'")
cnx.commit()
def face_cropped(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
# scaling factor=1.3
# Minimum neighbor = 5
if len(faces) == 0:
return None
for (x, y, w, h) in faces:
cropped_face = img[y:y + h, x:x + w]
return cropped_face
cap = cv2.VideoCapture(1)
time.sleep(0.5)
mycursor.execute("select ifnull(max(img_id), 0) from img_dataset")
row = mycursor.fetchone()
lastid = row[0]
img_id = lastid
max_imgid = img_id + 100
count_img = 0
while True:
ret, img = cap.read()
if not ret:
print("Error: Couldn't capture image from webcam.")
break
if face_cropped(img) is None:
frame1 = cv2.resize(img, (200, 200))
frame1 = cv2.imencode('.jpg', frame1)[1].tobytes()
yield (b'--frame1\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame1 + b'\r\n')
if face_cropped(img) is not None:
count_img += 1
img_id += 1
face = cv2.resize(face_cropped(img), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# Convert the image data to bytes
_, img_encoded = cv2.imencode('.jpg', face)
img_bytes = img_encoded.tobytes()
# Insert the image data into the database
mycursor.execute("""
INSERT INTO img_data (img_person, img_data)
VALUES (%s, %s)
""", (nbr, img_bytes))
cnx.commit()
file_name_path = "dataset/" + nbr + "." + str(img_id) + ".jpg"
cv2.imwrite(file_name_path, face)
cv2.putText(face, str(count_img) + '%', (5, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
mycursor.execute("""INSERT INTO `img_dataset` (`img_id`, `img_person`) VALUES
('{}', '{}')""".format(img_id, nbr))
cnx.commit()
if int(img_id) == int(max_imgid):
if int(img_id) == int(max_imgid):
cv2.putText(face, "Training Complete", (5, 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
cv2.putText(face, "Click Train Face.", (5, 45), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 255), 1)
frame = cv2.imencode('.jpg', face)[1].tobytes()
yield (b'--frame1\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
if cv2.waitKey(1) == 13 or int(img_id) == int(max_imgid):
break
cap.release()
cv2.destroyAllWindows()
@app.route('/train_classifier/<nbr>')
def train_classifier(nbr):
user_id = session.get('user_id') # Get the user's ID from the session
# dataset_dir = "C:/Users/jd/PycharmProjects/FlaskOpencv_FaceRecognition/dataset"
if not has_completed_training(user_id):
img_count = get_image_count(user_id) # Get the image count for the user
if img_count == 100:
dataset_dir = "dataset"
path = [os.path.join(dataset_dir, f) for f in os.listdir(dataset_dir)]
faces = []
ids = []
for image in path:
img = Image.open(image).convert('L');
imageNp = np.array(img, 'uint8')
id = int(os.path.split(image)[1].split(".")[1])
faces.append(imageNp)
ids.append(id)
ids = np.array(ids)
# Train the classifier and save
clf = cv2.face.LBPHFaceRecognizer_create()
clf.train(faces, ids)
clf.write("classifier.xml")
mycursor.execute("UPDATE users SET completed_training = 1 WHERE id = %s", (user_id,))
cnx.commit()
flash('TRAIN SUCCESSFUL.', 'success')
else:
flash('SORRY, TRAIN MUST BE 100%', 'danger')
else:
flash('YOU CAN ONLY TRAIN ONCE.', 'danger')
return redirect('/vfdataset_page')
字符串
这是我的错误代码:
[ WARN:email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)] global cap_v4l.cpp:982 open VIDEOIO(V4 L2:/dev/video 1):无法通过索引打开摄像头
[错误:email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)] global obsensor_uvc_stream_channel.cpp:156 getStreamStream组摄像机索引超出范围
我已经尝试改变cap = cv2.VideoCapture(1)
到-1,0,1,2,3,我的相机是完全工作.我希望有人能帮助我找出这个错误的可能解决方案.
1条答案
按热度按时间1bqhqjot1#
循环通过可用的相机试试这个:
字符串