尝试使用Keras检测情绪,用mss抓取桌面,然后将它们显示到OpenCV窗口。角膜模型尺寸为360毫巴。
import time
import cv2
import mss
import numpy as np
face_cascade = cv2.CascadeClassifier('face.xml')
label = ["angry", "happy", "sad", "stress"]
monitor = {"top": 0, "left": 0, "width": 1000, "height": 1000}
with mss.mss() as sct:
# Part of the screen to capture
while "Screen capturing":
# Get raw pixels from the screen, save it to a Numpy array
img = np.array(sct.grab(monitor))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 255), 2)
roi_gray = gray[y:y+h,x:x+w]
roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
roi = roi_gray.reshape(1, 48, 48, 1)
prediction = model.predict(roi)
t = label[prediction.argmax()]
label_position = (x,y)
cv2.putText(img,t,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
# Display the picture
cv2.imshow("OpenCV/Numpy normal", img)
#print("fps: {}".format(1 / (time.time() - last_time)))
# Press "q" to quit
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break是否有任何方法来加快这一过程或它的硬件限制?
发布于 2022-12-04 13:05:51
有几种可能加快这一进程的方法:
使用更小的模型:360 so对于Keras模型来说是相当大的,所以使用更小的模型和更少的层和参数可以提高性能。
使用更快的硬件:这个过程的速度可能是硬件绑定的,所以使用更快的CPU或GPU可以提高性能。
优化代码:可能有优化代码的方法,例如使用优化的函数进行图像处理、使用线程或多处理,或者使用更高效的数据结构来存储和处理数据。
减少处理的帧数:与其处理屏幕截图中的每一帧,不如跳过一些帧以减少工作负载。这可能导致较低的帧速率,但可能提高性能。
https://stackoverflow.com/questions/74675873
复制相似问题