我想從視頻中獲取每一幀作為圖像。背景如下。我寫了一個能夠識別手勢的神經網絡?,F在我想開始一個視頻流,其中流的每個圖像/幀都通過神經網絡。為了使其適合我的神經網絡,我想渲染每一幀并將圖像縮小到 28*28 像素。最后它看起來應該類似于:https ://www.youtube.com/watch?v= JfSao30fMxY 我已經通過網絡搜索并發現我可以使用 cv2.VideoCapture 來獲取流。但是我怎樣才能挑選幀的每個圖像,渲染它并將結果打印回屏幕上。到目前為止,我的代碼看起來像這樣:import numpy as npimport cv2cap = cv2.VideoCapture(0)# Todo: each Frame/Image from the video should be saved as a variable and open imageToLabel()# Todo: before the image is handed to the method, it needs to be translated into a 28*28 np Array# Todo: the returned Label should be printed onto the video (otherwise it can be )i = 0while (True): # Capture frame-by-frame # Load model once and pass it as an parameter ret, frame = cap.read() i += 1 image = cv2.imwrite('database/{index}.png'.format(index=i), frame) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRAY) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break# When everything done, release the capturecap.release()cv2.destroyAllWindows()def imageToLabel(imgArr, checkpointLoad): new_model = tf.keras.models.load_model(checkpointLoad) imgArrNew = imgArr.reshape(1, 28, 28, 1) / 255 prediction = new_model.predict(imgArrNew) label = np.argmax(prediction) return label
1 回答
神不在的星期二
TA貢獻1963條經驗 獲得超6個贊
frame是您從流中獲得的 RGB 圖像。 gray是灰度轉換后的圖像。我想您的網絡因其形狀而采用灰度圖像。因此,您需要先將圖像大小調整為 (28,28),然后將其傳遞給您的 imageToLabel 函數
resizedImg = cv2.resize(gray,(28,28))
label = imageToLabel(resizedImg,yourModel)
現在您知道了預測,您可以frame使用 eg繪制它cv2.putText(),然后繪制它返回的幀而不是frame
編輯:
如果你想為你的網絡使用部分圖像,你可以像這樣切片圖像:
slicedImg = gray[50:150,50:150]
resizedImg = cv2.resize(slicedImg,(28,28))
label = imageToLabel(resizedImg,yourModel)
如果你不太熟悉 python 中的索引,你可能想看看這個
此外,如果您希望它看起來像鏈接視頻中的樣子,您可以繪制一個從例如 (50,50) 到 (150,150) 的綠色矩形 (0,255,0)
cv2.rectangle(frame,(50,50),(150,150),(0,255,0))
添加回答
舉報
0/150
提交
取消
