我想使用连接到Raspberry Pi 4的摄像头来同时传输视频和检测物体。我已经能够使用这个设备拍照和录制视频。
然而,当我尝试在流上运行ResNet50分类器时,我面临着形状不一致的错误。
我请求您帮我找到问题可能源自的线路号。
错误和代码(相对简单)附在下面。
错误代码:ValueError: Input 0 of layer "resnet50" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, 1, 224, 224)
代码:
#!/usr/bin/python3
import time
import os
from datetime import datetime
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import CircularOutput
from PIL import Image
# Define the resolution for the low-resolution video stream
lsize = (320, 240)
# Load a pre-trained TensorFlow model
classifier = tf.keras.applications.resnet50.ResNet50(weights='imagenet')
# Initialize the Picamera2
picam2 = Picamera2()
# Configure the video settings
video_config = picam2.create_video_configuration(
main={"size": (1920, 1080), "format": "RGB888"},
lores={"size": lsize, "format": "YUV420"}
)
picam2.configure(video_config)
# Initialize the H.264 encoder for video capture
encoder = H264Encoder(2000000, repeat=True)
encoder.output = CircularOutput()
picam2.encoder = encoder
# Start the camera and the encoder
picam2.start()
picam2.start_encoder(encoder)
# Define the dimensions of the low-resolution frame
w, h = lsize
# Continuous loop to capture and process frames
while True:
# Capture a frame from the low-resolution stream
cur = picam2.capture_buffer("lores")
# Convert the frame to an image and
# preprocess it for the deep learning model
image = Image.fromarray(cur.astype('uint8'))
image = image.resize((224, 224)) # Resize to match the model's input size
image = np.array(image) / 255.0 # Normalize
image = np.expand_dims(image, axis=0) # Add batch dimension
# Run the deep learning model on the frame
predictions = classifier.predict(np.expand_dims(image, axis=-1))
# Print the model's predictions (customize as needed)
print(
"Model predictions class:",
tf.keras.applications.imagenet_utils.decode_predictions(
preds=predictions
)
)
time.sleep(1)
# Stop the camera and the encoder
picam2.stop()
picam2.stop_encoder()
1条答案
按热度按时间mec1mxoz1#
我把
lsize
改成了(160, 120)
,得双曲正弦值.
工作代码:
PS:特别感谢https://stackoverflow.com/users/12317368/ro-ot。