【发布时间】:2017-11-10 01:01:50
【问题描述】:
我正在构建一些代码来自适应地检测网络摄像头视频中的皮肤。我几乎可以正常工作,但是,在输出视频时,它显示了 9 个“皮肤”蒙版屏幕,而不仅仅是一个。好像我只是错过了一些简单的东西,但我无法弄清楚。
代码如下:
# first let's train the data
data, labels = ReadData()
classifier = TrainTree(data, labels)
# get the webcam. The input is either a video file or the camera number
# since using laptop webcam (only 1 cam), input is 0. A 2nd cam would be input 1
camera = cv2.VideoCapture(0)
while True:
# reads in the current frame
# .read() returns True if frame read correctly, and False otherwise
ret, frame = camera.read() # frame.shape: (480,640,3)
if ret:
# reshape the frame to follow format of training data (rows*col, 3)
data = np.reshape(frame, (frame.shape[0] * frame.shape[1], 3))
bgr = np.reshape(data, (data.shape[0], 1, 3))
hsv = cv2.cvtColor(np.uint8(bgr), cv2.COLOR_BGR2HSV)
# once we have converted to HSV, we reshape back to original shape of (245057,3)
data = np.reshape(hsv, (hsv.shape[0], 3))
predictedLabels = classifier.predict(data)
# the AND operator applies the skinMask to the image
# predictedLabels consists of 1 (skin) and 2 (non-skin), needs to change to 0 (non-skin) and 255 (skin)
predictedMask = (-(predictedLabels - 1) + 1) * 255 # predictedMask.shape: (307200,)
# resize to match frame shape
imgLabels = np.resize(predictedMask, (frame.shape[0], frame.shape[1], 3)) # imgLabels.shape: (480,640,3)
# masks require 1 channel, not 3, so change from BGR to GRAYSCALE
imgLabels = cv2.cvtColor(np.uint8(imgLabels), cv2.COLOR_BGR2GRAY) # imgLabels.shape: (480,640)
# do bitwsie AND to pull out skin pixels. All skin pixels are anded with 255 and all others are 0
skin = cv2.bitwise_and(frame, frame, mask=imgLabels) # skin.shape: (480,640,3)
# show the skin in the image along with the mask, show images side-by-side
# **********THE BELOW LINE OUTPUTS 9 screens of the skin mask instead of just 1 ****************
cv2.imshow("images", np.hstack([frame, skin]))
# if the 'q' key is pressed, stop the loop
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
# release the video capture
camera.release()
cv2.destroyAllWindows()
【问题讨论】:
-
为什么不打印结果的形状?
-
我编辑了我的代码以添加每个变量的形状(见上文)。 predictMask 为 307200(即 rows*cols),resize 后的 imgLabels 大于 307200,变为 (480,640,3)。也许这是导致错误的原因?但是,将imgLabels输入到cvtColor BGR到GRAY需要3的颜色通道。我尝试将调整大小更改为将 imgLabels 输出为 (480,640,1),然后注释掉转换为 GRAY,但最终出现此错误:“error: (-215) (mtype == CV_8U || mtype == CV_8S ) && _mask.sameSize(*psrc1) in function cv::binary_op" 任何帮助表示赞赏!
-
我不知道给分类器喂什么。但我认为您转换颜色的方法是错误的。而且程序不完整,无法调试。
标签: python python-3.x opencv