Почему в cv2 нет атрибута DestroyAllWindows?Python

Программы на Python
Anonymous
Почему в cv2 нет атрибута DestroyAllWindows?

Сообщение Anonymous »

Я пытаюсь написать программу для распознавания лиц с использованием OpenCV. Это код в файле facerec.py,

Код: Выделить всё

#!/usr/bin/env python

import cv2
import face_recognition
import os
import numpy as np
from datetime import datetime
import pickle
import re

def MainFunc():

path = './student_images' #specify the path to the directory containing student images
images = []
className = []
usnNum = []

mylist = os.listdir(path) #get a list of image files in the specified directory

#loop through each image file in the directory
for cl in mylist:
curImg = cv2.imread(f'{path}/{cl}') #read the image file
images.append(curImg)
Fname_ext = os.path.splitext(cl)[0] #extract the class name (student's name) from the file name without the extension
Fname = re.split('_', Fname_ext)
className.append(Fname[0]) #append the Name
usnNum.append(Fname[1]) #append the USN

#className.append(os.path.splitext(cl)[0]) #extract the class name (student's name) from the file name without the extension

#define a function to find face encodings from a list of images
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #convert the images from BGR format to RGB format (required by face_recognition)
encoded_face = face_recognition.face_encodings(img)[0] #encode the face in the image
encodeList.append(encoded_face)
return encodeList

encoded_face_train = findEncodings(images) #get the face encodings for the known student images

#define a function to mark attendance
def markAttendance(name,usn):
with open('Attendance.csv', 'r+') as f: #open the attendance csv file in the read and write mode
myDataList = f.readlines()
nameList = []

for line in myDataList: #extract existing names from the csv file
entry = line.split(',')
nameList.append(entry[0])

if name not in nameList: #if the name is not in the csv file, add a new entry with the current time and date
now = datetime.now()
time = now.strftime('%I:%M:%S %p')
date = now.strftime('%d-%B-%Y')
f.writelines(f'\n{name}, {usn}, {time}, {date}')

cap = cv2.VideoCapture(0) #open the webcam (camera)

while True:
success, img = cap.read() #capture a frame from the webcam
if not success: #edit using success to see if it is actually reading the image.
print('Failed to grab frame')
break
imgS = cv2.resize(img, (0,0), None, 0.25, 0.25) #resize the captured frame to 1/4 of its original size for faster processing
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB) #covert the resized frame from bgr to rgb format
faces_in_frame = face_recognition.face_locations(imgS) #detect faces in the resized frame
encoded_faces = face_recognition.face_encodings(imgS, faces_in_frame) #encode the detected faces

for encoded_face, faceloc in zip(encoded_faces, faces_in_frame): #loop through each detected face in the frame
matches = face_recognition.compare_faces(encoded_face_train, encoded_face) #compare the detected face with known faces
faceDist = face_recognition.face_distance(encoded_face_train, encoded_face) #calculate the face distance to find the best match
matchIndex = np.argmin(faceDist)

if matches[matchIndex]: #if a match is found, mark attendance and display the name
name = className[matchIndex].upper().lower()
usn = usnNum[matchIndex].upper().lower()
y1, x2, y2, x1 = faceloc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4 #scale coordinates back to original size
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2) #draw a rectangle around the detected face
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (255, 0, 0), cv2.FILLED) #draw a filled rectangle for displaying the name
cv2.putText(img, name, (x1 + 6, y2 - 5), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 2) #display the name on the frame
markAttendance(name,usn) #call the markAttendacne function to record attendance

cv2.imshow('webcam', img) #display the frame with detected faces and names

if cv2.waitKey(1) &  0xFF == ord('q'):
print("Q hit, closing...")
break

cap.release()
cv2.destoryAllWindows()

if __name__ == '__main__':
MainFunc()

Я получаю сообщение об ошибке:

Код: Выделить всё

Traceback (most recent call last):
File "facerec.py", line 97, in 
MainFunc()
File "facerec.py", line 94, in MainFunc
cv2.destoryAllWindows()
^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'cv2' has no attribute 'destoryAllWindows'
Я запускаю это в виртуальной среде, и установлены следующие пакеты:

Код: Выделить всё

Package                 Version
----------------------- ---------
click                   8.1.7
dlib                    19.24.4
face-recognition        1.3.0
face_recognition_models 0.3.0
greenlet                3.0.3
msgpack                 1.0.8
numpy                   1.26.4
opencv-python           4.10.0.82
packaging               24.1
pillow                  10.3.0
pip                     24.0
setuptools              70.0.0
Это то, что я пытался сделать, так как я не мог найти много информации об этом в Google, я прокомментировал cv2.destroyAllWindows(), но теперь есть ошибка типа,

Код: Выделить всё

Traceback (most recent call last):
File "facerec.py", line 102, in 
MainFunc()
File "facerec.py", line 67, in MainFunc
imgS = cv2.resize(img, (0,0), None, 0.25, 0.25) #resize the captured frame to 1/4 of its original size for faster processing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.10.0) /io/opencv/modules/imgproc/src/resize.cpp:4152: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
Я безуспешно пытался следовать ответам на ранее заданный вопрос. Запускаю программу на Linux, до перезагрузки код работал хорошо, не могу понять, что изменилось. Любая помощь очень ценится.

Подробнее здесь: https://stackoverflow.com/questions/786 ... allwindows

Вернуться в «Python»