gpt4 book ai didi

python - CV2:关闭相机连接并在不同功能中重新打开

转载 作者:行者123 更新时间:2023-12-01 08:48:55 26 4
gpt4 key购买 nike

所以基本上我在这个脚本中有两个函数。 1 进行人脸识别并显示数据库中存储的数据,第二个在看到无法识别的人脸时触发,并存储临时数据。问题是,当触发第二个时,我总是收到错误

VIDEOIO ERROR can't open camera by index 1

Unable to stop the stream: Device or resource busy

我很确定我在face_recog函数中正确释放了它,但我不确定。如有任何帮助,我们将不胜感激。

video_capture = cv2.VideoCapture(1)

configured_datetime = str(datetime.datetime.now().strftime('%c'))

def gather_data():

faceDetect1=cv2.CascadeClassifier('frontal_cascade_improved.xml')
faceDetect2=cv2.CascadeClassifier('profilecascade.xml')



# #Input Info
# name = input('Input Name: ')
# position = input('Position/Title: ')

# Generate info later
name = 'Uknown Entity ' + ' Seen ' + configured_datetime
position = 'Unknown Position'

directory_path = (path + '/' + name)
os.makedirs(directory_path, exist_ok=True)
sampleNum = 0

while(True):
video_capture = cv2.VideoCapture(1)
ret,img=video_capture.read()
# gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect1.detectMultiScale(img)
for(x,y,w,h) in faces:
sampleNum=sampleNum+1;
cv2.imwrite(directory_path + '/' + str(name) + '.' + str(sampleNum) +'.jpg', img)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.waitKey(100);
cv2.imshow("Face",img);
cv2.waitKey(1);
if(sampleNum>6):
break;
database_info = Faces(name=name, image_repository=str(directory_path), position=position)
db.session.add(database_info)
db.session.commit()
video_capture.release()
cv2.destroyAllWindows()


def face_recog():


# try:

image_info = Faces.query.all()
known_face_encodings = []
known_face_names = []

#manually set number of images, make it match sampleNum - more images DOESN'T increase recognizability---

for num, person in enumerate(image_info, 1):
# Go thru all iamges - doesn't improve recog
# for i, x in enumerate(range(6), 1):
image_file = str('datasets/' + person.name + '/' + person.name + '.' + str(1) + '.jpg')
print(image_file)
image_base = face_recognition.load_image_file(image_file)
image_encoding = face_recognition.face_encodings(image_base)[0]
known_face_encodings.append(image_encoding)
known_face_names.append(person.name)

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
image_info = Faces.query.all()
# Grab a single frame of video
ret, frame = video_capture.read()

# Resize frame of video to 1/4 size for faster face recognition processing
# small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = frame

# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"

# If a match was found in known_face_encodings, just use the firsst one.
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]

face_names.append(name)

process_this_frame = not process_this_frame


# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 1
right *= 1
bottom *= 1
left *= 1


if name == 'Unknown':
video_capture.release()
gather_data()
elif 'Entity' in name:
cv2.rectangle(frame, (left, top), (right, bottom), (19, 198, 192), 2)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 30, bottom + 20), font, 0.5, (0, 0, 0), 3)
cv2.putText(frame, name, (left + 30, bottom + 20), font, 0.5, (19, 198, 192), 1)
else:
person = Faces.query.filter_by(name=name).first()
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
# Draw a label and text
cv2.rectangle(frame, (left, bottom - 12), (right, bottom), (0,255, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left - 7, bottom + 20), font, 0.75, (0, 0, 0), 3)
cv2.putText(frame, name, (left - 7, bottom + 20), font, 0.75, (255, 255, 255), 1)
cv2.putText(frame, person.position, (left - 7, bottom + 45), font, 0.75, (0, 0, 0), 3)
cv2.putText(frame, person.position, (left - 7, bottom + 45), font, 0.75, (255, 255, 255), 1)


# Display the resulting image
cv2.imshow('Video', frame)

# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

编辑:修复了格式,只是想添加测试各种事物时存在多余的代码,但除了传递相机的使用之外,它是有效的。

最佳答案

通过将 video_capture 变量从face_recog 函数传递到gather_data 解决了该问题。因此,除了face_recog 内部之外,所有对实际视频设备的引用都消失了。其中一个部分看起来像..

if name == 'Unknown':
gather_data(video_capture)

编辑:哦,我只是在gather_data结束时再次调用face_recog()

关于python - CV2:关闭相机连接并在不同功能中重新打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com