body = str(bytes_list, encodings='utf-8')
时间: 2023-12-07 22:02:42 浏览: 43
预测长久物流的超额收益率。这里,我们使用随机森林模型进行预测。
代码这个错误提示表明在将字节串解码为字符串时,使用了无效的关键字参数 `encodings如下:
```python
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
# 将数据集分为训练集和测试集
train_size = int(len(XY) * 0.7)
train_XY, test_XY =`。`str()` 函数不接受 `encodings` 这个参数。
你可以使用 `decode()` 方法将字节串解 XY[:train_size], XY[train_size:]
# 分离自变量和因变量
train_X, train_y = train_XY码为字符串,例如:
```
body = bytes_list.decode('utf-8')
```
这将会使用 UTF-8 编码方式将字节串解码为字符串。
相关问题
conn.send(bytes(response_str,encodings='utf-8'))
这个错误提示表明在使用 `send()` 方法发送数据时,使用了无效的关键字参数 `encodings`。`send()` 方法不接受 `encodings` 这个参数。
你可以使用 `encode()` 方法将字符串编码为字节串,然后再使用 `send()` 方法发送字节串,例如:
```
conn.send(response_str.encode('utf-8'))
```
这将会使用 UTF-8 编码方式将字符串编码为字节串,然后发送字节串。
import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw,ImageFont video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4')#如果输入是(0)为摄像头输入 #现输入为MP4进行识别检测人脸 first_image = face_recognition.load_image_file("1.jpg") first_face_encoding = face_recognition.face_encodings(first_image)[0] Second_image = face_recognition.load_image_file("2.jpg") Second_face_encoding = face_recognition.face_encodings(Second_image)[0] third_image = face_recognition.load_image_file("3.jpg") third_face_encoding = face_recognition.face_encodings(third_image)[0] inside_face_encodings = [first_face_encoding,Second_face_encoding,third_face_encoding] inside_face_names = ['A','B','C'] face_locations = [] face_encodings = [] face_names = [] process_this_frame = True 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] 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(inside_face_encodings, face_encoding) name = '未录入人脸' if True in matches: first_match_index = matches.index(True) name = inside_face_names[first_match_index] face_names.append(name) process_this_frame = not process_this_frame 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, 255), 2) img_pil = Image.fromarray(frame) draw = ImageDraw.Draw(img_pil) fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8") draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle) frame = np.asarray(np.array(img_pil)) cv2.imshow('face_out', frame) if cv2.waitKey(1) & 0xFF == ord('q'): #退出需要按下Q键否则内核会崩溃 break video_capture.release() cv2.destroyAllWindows()
以下是修改后的代码,主要是添加了注释和一些规范化的修改:
```python
import cv2
import face_recognition
import numpy as np
from PIL import Image, ImageDraw, ImageFont
# 加载要识别的人脸图片
first_image = face_recognition.load_image_file("1.jpg")
first_face_encoding = face_recognition.face_encodings(first_image)[0]
Second_image = face_recognition.load_image_file("2.jpg")
Second_face_encoding = face_recognition.face_encodings(Second_image)[0]
third_image = face_recognition.load_image_file("3.jpg")
third_face_encoding = face_recognition.face_encodings(third_image)[0]
# 存储要检测的人脸编码及对应的人名
inside_face_encodings = [first_face_encoding, Second_face_encoding, third_face_encoding]
inside_face_names = ['A', 'B', 'C']
# 打开视频流
video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4')
# 循环读取视频帧
while True:
# 读取一帧
ret, frame = video_capture.read()
# 降低帧率,加快处理速度
if not ret:
break
# 缩小图片尺寸,加快处理速度
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
# 处理每一帧图片
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(inside_face_encodings, face_encoding)
name = '未录入人脸'
# 找到匹配的人脸
if True in matches:
first_match_index = matches.index(True)
name = inside_face_names[first_match_index]
face_names.append(name)
# 在画面上标记人脸及名字
for (top, right, bottom, left), name in zip(face_locations, face_names):
# 将坐标放大4倍,因为之前缩小了4倍
top *= 4
right *= 4
bottom *= 4
left *= 4
# 画出人脸框
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# 添加名字
img_pil = Image.fromarray(frame)
draw = ImageDraw.Draw(img_pil)
fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8")
draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle)
frame = np.asarray(np.array(img_pil))
# 显示画面
cv2.imshow('face_out', frame)
# 按下q键退出程序
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放视频流
video_capture.release()
# 关闭所有窗口
cv2.destroyAllWindows()
```
阅读全文