CV2:关闭网络摄像头并以不同的功能重新打开
在此代码中,我想使用 open cv 和face_recognition 显示 3 种类型的分类:“已知”、“未知”和“敌对”。 第一个函数(firtsCase())包含“已知”、“未知”并且一切正常。 我想做的是:当程序知道函数 secondaryCase() 中的函数是“敌对”并且我按“q”时,网络摄像头必须关闭并启动将我发送回“已知”案例的函数,并且“未知”(在 secondaryCase() 末尾与firstCase() 一起调用)重新打开网络摄像头并重新分类。 您可以在函数 SecondCase() 的最新部分中找到它。
我有这个错误:
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) cv2.error:OpenCV(4.5.5)/Users/runner/work/opencv-python/opencv-python/opencv/modules/imgproc/src/resize.cpp:4052:错误:(-215:断言失败)!ssize函数“resize”中的 .empty()
我知道这个错误出现是因为它试图调整已经被操纵的东西的大小,但我不知道知道我该如何解决它。
这是代码:
video_capture = cv2.VideoCapture(0)
face2 = face_recognition.load_image_file(os.path.abspath("path2"))
face2_face_encoding = face_recognition.face_encodings(face2)[0]
known_face_encodings = [
face2_face_encoding
]
known_face_names = [
"Giulia"
]
face_location = []
face_encodings = []
face_names = []
def firstCase():
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
process_this_frame = True
face = False
if process_this_frame:
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:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.6)
name = "Sconosciuto"
face = True
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face = False
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
face_names.append(name)
process_this_frame = not process_this_frame
if face:
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (40,48,48), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (40,48,48), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.6, (255, 255, 255), 1)
cv2.imshow('Video', frame)
else:
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 255, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.6, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
def secondCase():
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
process_this_frame = True
face = False
if process_this_frame:
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:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.6)
name = "Ostile"
face = True
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face = False
face_names.append(name)
process_this_frame = not process_this_frame
if face:
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0,0,0), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0,0,0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.6, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
firstCase()
print("Inizio scansione :" + time.ctime() + "\nCitofono attivo. Citofona premendo a... ")
sel = selectors.DefaultSelector()
sel.register(sys.stdin, selectors.EVENT_READ)
sys.stdout.flush()
pairs = sel.select(timeout=5)
if pairs:
if input("Citofona premendo 'a' \n"):
firstCase()
else:
secondCase()
谢谢大家的帮助。
In this code I want to display 3 types of classification: "Known", "Unknown" and "Hostile" using open cv and face_recognition.
The first function (firtsCase()) contains "Known", "Unknown" and that it's everything ok.
What I want to do is: when the program knows that is in the function secondCase() that is "hostile" and I press "q", the webcam must close and launch the function that sends me back to the cases "Known" and "Unknown" (call at the end of secondCase() with firstCase()) to reopen the webcam and reclassify.
You can find it in the latest part of function secondCase().
I have this error:
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
cv2.error: OpenCV(4.5.5) /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgproc/src/resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
I know this error shows up because it is trying to resize something that has already been manipulated, but I don't know how can I fix it.
This is the code:
video_capture = cv2.VideoCapture(0)
face2 = face_recognition.load_image_file(os.path.abspath("path2"))
face2_face_encoding = face_recognition.face_encodings(face2)[0]
known_face_encodings = [
face2_face_encoding
]
known_face_names = [
"Giulia"
]
face_location = []
face_encodings = []
face_names = []
def firstCase():
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
process_this_frame = True
face = False
if process_this_frame:
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:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.6)
name = "Sconosciuto"
face = True
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face = False
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
face_names.append(name)
process_this_frame = not process_this_frame
if face:
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (40,48,48), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (40,48,48), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.6, (255, 255, 255), 1)
cv2.imshow('Video', frame)
else:
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 255, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.6, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
def secondCase():
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
process_this_frame = True
face = False
if process_this_frame:
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:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.6)
name = "Ostile"
face = True
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face = False
face_names.append(name)
process_this_frame = not process_this_frame
if face:
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0,0,0), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0,0,0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.6, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
firstCase()
print("Inizio scansione :" + time.ctime() + "\nCitofono attivo. Citofona premendo a... ")
sel = selectors.DefaultSelector()
sel.register(sys.stdin, selectors.EVENT_READ)
sys.stdout.flush()
pairs = sel.select(timeout=5)
if pairs:
if input("Citofona premendo 'a' \n"):
firstCase()
else:
secondCase()
Thank you all for helping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论