def init_known_face(self): # 数据库连接对象 conn = MySQLdb.connect(host = 'localhost',port=3306 ,user = 'root',passwd = 'root',db = 'attendanceinfo',charset='utf8') # 游标对象 cur = conn.cursor() # 查询的sql语句 sql = "SELECT * FROM userinfo " cur.execute(sql) data = cur.fetchall() row=cur.rowcount vol=6 self.stopvidow = False self.known_face_encodings = [] self.known_face_names = [] for i in range(row): is_exsit = os.path.exists(str(data[i][4])) if (is_exsit): obama_image = face_recognition.load_image_file(str(data[i][4])) obama_face_encoding = self.new_method(obama_image) self.known_face_encodings.append(obama_face_encoding) self.known_face_names.append(str(data[i][1])) cur.close() self.new_method(conn) self.new_method2() def new_method(self, conn): conn.close() def new_method1(self, obama_image): obama_face_encoding = face_recognition.face_encodings(obama_image)[0] return obama_face_encoding找出代码错误并修正
时间: 2024-02-14 22:08:05 浏览: 206
mysql_read.rar_Python MySQLdb_connect_python
在这段代码中,`self.new_method` 和 `self.new_method2` 方法没有被正确命名,应该修改为正确的方法名。同时,`self.new_method` 方法的作用是关闭数据库连接,但是在初始化已知人脸编码时被错误调用,应该将其修改为一个专门关闭数据库连接的方法。
修改后的代码如下:
```python
import face_recognition
import MySQLdb
import os
class FaceRecognizer:
def __init__(self):
self.stopvidow = False
self.known_face_encodings = []
self.known_face_names = []
self.init_known_face()
def init_known_face(self):
# 数据库连接对象
conn = MySQLdb.connect(host='localhost', port=3306, user='root',
passwd='root', db='attendanceinfo', charset='utf8')
# 游标对象
cur = conn.cursor()
# 查询的sql语句
sql = "SELECT * FROM userinfo "
cur.execute(sql)
data = cur.fetchall()
row = cur.rowcount
vol = 6
self.known_face_encodings = []
self.known_face_names = []
for i in range(row):
is_exsit = os.path.exists(str(data[i][4]))
if (is_exsit):
obama_image = face_recognition.load_image_file(str(data[i][4]))
obama_face_encoding = self.get_face_encoding(obama_image)
self.known_face_encodings.append(obama_face_encoding)
self.known_face_names.append(str(data[i][1]))
cur.close()
conn.close()
def get_face_encoding(self, obama_image):
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
return obama_face_encoding
def new_method2(self):
pass
```
注意,我将 `new_method` 修改为 `get_face_encoding`,并在 `init_known_face` 方法中调用它来获取人脸编码。此外,我也将 `new_method2` 修改为一个空方法。
阅读全文