def Getbinary(frame, x0, y0, width, height, finger_model): # 显示方框 # cv2.rectangle(frame, (x0, y0), (x0+width, y0+height), (0, 255, 0)) res = pic.new_binaryMask(frame, x0, y0, width, height) #提取ROI像素 # roi = frame[y0:y0+height, x0:x0+width] # # gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) # 高斯模糊 斯模糊本质上是低通滤波器,输出图像的每个像素点是原图像上对应像素点与周围像素点的加权和 # 高斯矩阵的尺寸越大,标准差越大,处理过的图像模糊程度越大 # blur = cv2.GaussianBlur(gray, (5, 5), 2) # 高斯模糊,给出高斯模糊矩阵和标准差 # 当同一幅图像上的不同部分的具有不同亮度时。这种情况下我们需要采用自适应阈值 # 参数: src 指原图像,原图像应该是灰度图。 x :指当像素值高于(有时是小于)阈值时应该被赋予的新的像素值 # adaptive_method 指: CV_ADAPTIVE_THRESH_MEAN_C 或 CV_ADAPTIVE_THRESH_GAUSSIAN_C # block_size 指用来计算阈值的象素邻域大小: 3, 5, 7, .. # param1 指与方法有关的参数 # # th3 = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) # ret, res = cv2.threshold(th3, 70, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) # ret还是bool类型 out = 0 """这里可以插入代码调用网络""" test_image = res test_image = cv.resize(test_image, (100, 100)) test_image = np.array(test_image, dtype='f') test_image = test_image / 255.0 test_image = test_image.reshape([-1, 100, 100, 1]) pdt = finger_model.predict(test_image) out = np.argmax(pdt, axis=1) cv2.putText(frame, "the finger is: %d" % out, (x0, y0), font, fontsize, (0, 255, 0)) # 标注字体 return res
def binaryMask(frame, x0, y0, width, height): # 显示方框 # cv2.rectangle(frame, (x0, y0), (x0+width, y0+height), (0, 255, 0)) # 提取ROI像素 roi = frame[y0:y0+height, x0:x0+width] # res = pic.new_binaryMask(frame,x0,y0,width,height) # res = fun(roi) # gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) # # 高斯模糊 斯模糊本质上是低通滤波器,输出图像的每个像素点是原图像上对应像素点与周围像素点的加权和 # # 高斯矩阵的尺寸越大,标准差越大,处理过的图像模糊程度越大 # blur = cv2.GaussianBlur(gray, (5, 5), 2) # 高斯模糊,给出高斯模糊矩阵和标准差 # # # 当同一幅图像上的不同部分的具有不同亮度时。这种情况下我们需要采用自适应阈值 # # 参数: src 指原图像,原图像应该是灰度图。 x :指当像素值高于(有时是小于)阈值时应该被赋予的新的像素值 # # adaptive_method 指: CV_ADAPTIVE_THRESH_MEAN_C 或 CV_ADAPTIVE_THRESH_GAUSSIAN_C # # block_size 指用来计算阈值的象素邻域大小: 3, 5, 7, .. # # param1 指与方法有关的参数 # # th3 = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) # ret, res = cv2.threshold(th3, 70, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) # ret还是bool类型 # 保存手势 if saveImg == True and binaryMode == True: saveROI(res) elif saveImg == True and binaryMode == False: saveROI(roi) """这里可以插入代码调用网络""" return res
def Getbinary(frame, x0, y0, width, height, finger_model): # 得到处理后的照片 res = pic.new_binaryMask(frame, x0, y0, width, height) out = 0 """这里可以插入代码调用网络""" test_image = res test_image = cv.resize(test_image, (300, 300)) test_image = np.array(test_image, dtype='f') test_image = test_image / 255.0 test_image = test_image.reshape([-1, 300, 300, 1]) pdt = finger_model.predict(test_image) out = np.argmax(pdt, axis=1) cv2.putText(frame, "the finger is: %d" % out, (x0, y0), font, fontsize, (0, 0, 255)) # 标注字体 return frame,res
def Getresult(self, frame, x0, y0, width, height, finger_model): # 得到处理后的照片 res = pic.new_binaryMask(frame, x0, y0, width, height) return self.Getvalue(frame, res, finger_model)
import cv2 import picture as pic font = cv2.FONT_HERSHEY_SIMPLEX # 设置字体 size = 0.5 # 设置大小 width, height = 300, 300 # 设置拍摄窗口大小 x0, y0 = 330, 50 # 设置选取位置 cap = cv2.VideoCapture(0) # 开摄像头 if __name__ == "__main__": while (1): ret, frame = cap.read() # 读取摄像头的内容 frame = cv2.flip(frame, 2) roi = pic.new_binaryMask(frame, x0, y0, width, height) # 取手势所在框图并进行处理 key = cv2.waitKey(1) & 0xFF # 按键判断并进行一定的调整 # 按'j''l''u''j'分别将选框左移,右移,上移,下移 # 按'q'键退出录像 if key == ord('i'): y0 += 5 elif key == ord('k'): y0 -= 5 elif key == ord('l'): x0 += 5 elif key == ord('j'): x0 -= 5 if key == ord('q'): break cv2.imshow('frame', frame) # 播放摄像头的内容 cap.release()