python 解决mysql where in 对列表(list,,array)问题

其实在很多实际开发中都会用到where xxx in (list)的查询语句进行数据查询,一般接口传参为了方便都会直接传递数组,那么该如何快捷的格式化sql语句呢

实例:
SELECT * FROM test where status='SUCESS' and operator_id in ('5122','5123');

前台传给过来的参数:

{
	"status": "SUCESS",
	"operator_id": ['5122','5123']
}
格式化:
filter_ret = ','.join(["'%s'" % item for item in operator_id])
sql = f"SELECT * FROM test where status='{status}' and operator_id in ({filter_ret})"
首先,需要使用dlib人脸检测器和人脸关键点检测器来提取人脸图片的特征值。然后,可以使用PythonMySQL连接器将这些特征值保存在MySQL数据库中。 以下是一个简单的示例代码,演示如何提取人脸图片的特征并将其保存在MySQL数据库中: ```python import cv2 import dlib import numpy as np import mysql.connector # 加载dlib人脸检测器和人脸关键点检测器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 连接到MySQL数据库 mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase" ) # 创建MySQL表格来存储人脸特征值 mycursor = mydb.cursor() mycursor.execute("CREATE TABLE IF NOT EXISTS faces (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), features BLOB)") # 用dlib提取人脸图片的特征值并将其保存到MySQL数据库中 def save_face_features(name, img_path): img = cv2.imread(img_path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray) for face in faces: landmarks = predictor(gray, face) features = np.array(landmarks.parts()[17:]).flatten().tolist() features_blob = bytes(features) sql = "INSERT INTO faces (name, features) VALUES (%s, %s)" val = (name, features_blob) mycursor.execute(sql, val) mydb.commit() # 调用save_face_features函数来保存人脸图片的特征值 save_face_features("John", "john.jpg") # 从MySQL数据库中获取人脸特征值并将其用于人脸识别 def recognize_face(img_path): img = cv2.imread(img_path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray) for face in faces: landmarks = predictor(gray, face) features = np.array(landmarks.parts()[17:]).flatten().tolist() features_blob = bytes(features) sql = "SELECT name FROM faces WHERE features = %s" val = (features_blob,) mycursor.execute(sql, val) result = mycursor.fetchone() if result: name = result[0] cv2.putText(img, name, (face.left(), face.top() - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow("Face Recognition", img) cv2.waitKey(0) cv2.destroyAllWindows() # 调用recognize_face函数来识别人脸并显示其名字 recognize_face("test.jpg") ``` 这个示例代码演示了如何使用Python和OpenCV和dlib将人脸图片的特征值保存在MySQL数据库中,并在识别时调用这些特征值来显示人脸图片的人名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值