How can i resize d435's depth frame?
Hi, im making image classifier with d435. While i do my project there are some problem with our code.
import dlib
import cv2
import pyrealsense2 as rs
import numpy as np
from keras.models import load_model
import os
import tensorflow as tf
from keras.preprocessing import image
def Rotate(src, degrees):
if degrees == 90:
dst = cv2.transpose(src)
dst = cv2.flip(dst, 1)
elif degrees == 180:
dst = cv2.flip(src, -1)
elif degrees == 270:
dst = cv2.transpose(src)
dst = cv2.flip(dst, 0)
else:
dst = null
return dst
test_model = load_model("capstone.h5")
if __name__ == "__main__":
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
pipeline.start(config)
detector = dlib.get_frontal_face_detector()
color_green = (0, 255, 0)
color_red = (0, 0, 255)
line_width = 3
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
colorizer = rs.colorizer()
colorized_depth = np.asanyarray(colorizer.colorize(depth_frame).get_data())
x=image.img_to_array(colorized_depth)
x=np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = test_model.predict(images)
if classes[0][0] == 1:
output = "Front"
elif classes[0][1] == 1:
output = "Left"
elif classes[0][2] == 1:
output = "right"
else:
output = "Wrong detection"
rgb_image = np.asanyarray(color_frame.get_data())
dst_image = Rotate(rgb_image, 270)
# Detection
dets = detector(dst_image)
for det in dets:
if ((det.left()+det.right())//2 - 3*((det.right()-det.left())//2))<=5:
left_end = 5
else:
left_end = (det.left()+det.right())//2 - 3*((det.right()-det.left())//2)
if ((det.left()+det.right())//2 + 5*((det.right()-det.left())//2))>=475:
right_end = 475
else:
right_end = (det.left()+det.right())//2 + 3*((det.right()-det.left())//2)
top_end = det.top() + 10
if (det.top() - 10)<=5:
top_end = 5
else:
top_end = det.top() - 10
if (det.bottom()*10)>=635:
bottom_end = 635
else:
bottom_end = det.bottom()*10
cv2.rectangle(dst_image, (left_end,top_end), (right_end,bottom_end), color_green, line_width)
cv2.putText(dst_image, output, (left_end, top_end-5), cv2.FONT_HERSHEY_DUPLEX, 1, color_green)
cv2.imshow('my webcam', dst_image)
key = cv2.waitKey(1)
# Press esc or 'q' to close the image window
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
# Stop streaming
pipeline.stop()
this is our code and the model were trained with colorized image with resized to (150,150).
i think if we can change depth image's frame size, then the model will work. this is our error message
ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 18496 but received input with shape (None, 289536)
-
Hi Blake58 As you are already using cv, an appropriate way to perform image resizing in your project may be to use cv2.resize.
https://www.geeksforgeeks.org/image-resizing-using-opencv-python/
The SDK Python example opencv_viewer_example.py demonstrates use of this command.
-
good to see you again Marty, I tried cv2.resize, but, it did'nt worked. Bunt, i found other solution of this.
my tensorflow model's input size was 150 X 150 size. so we changed input size of model from this
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])to this
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(640, 480,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])
then, it worked. Thanks for reply
Please sign in to leave a comment.
Comments
3 comments