Anupam251272's picture
Create app.py
6781c17 verified
raw
history blame
3.92 kB
import gradio as gr
import cv2
import torch
import numpy as np
import time
# Load the YOLO model on the CPU
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
def move_robot(command):
"""
Simulate sending a command to the robot to move.
Replace this with actual communication code to send commands to the robot.
"""
if command == "forward":
print("Robot moving forward")
elif command == "backward":
print("Robot moving backward")
elif command == "left":
print("Robot turning left")
elif command == "right":
print("Robot turning right")
elif command == "stop":
print("Robot stopped")
return f"Command sent: {command}"
def process_camera_feed():
"""Function to process single frame and run object detection"""
try:
# Initialize camera
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if not cap.isOpened():
print("Error: Could not open camera")
return np.zeros((480, 640, 3), dtype=np.uint8)
# Read a frame
ret, frame = cap.read()
if ret:
# Run YOLO detection
results = model(frame)
processed_frame = results.render()[0]
# Convert BGR to RGB for display
processed_frame = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
else:
processed_frame = np.zeros((480, 640, 3), dtype=np.uint8)
# Release the camera
cap.release()
return processed_frame
except Exception as e:
print(f"Error processing camera feed: {e}")
return np.zeros((480, 640, 3), dtype=np.uint8)
def continuously_update_feed():
"""Function to continuously update the camera feed"""
while True:
yield process_camera_feed()
time.sleep(0.1) # 100ms delay between updates
def robot_interface():
with gr.Blocks() as interface:
with gr.Row():
# Left column for controls
with gr.Column(scale=1):
# Control buttons
forward_btn = gr.Button("Forward", variant="primary")
backward_btn = gr.Button("Backward", variant="primary")
left_btn = gr.Button("Left", variant="primary")
right_btn = gr.Button("Right", variant="primary")
stop_btn = gr.Button("Stop", variant="secondary")
status_output = gr.Textbox(label="Robot Status")
# Right column for camera feed
with gr.Column(scale=2):
# Camera feed output
camera_output = gr.Image(
label="Robot Camera Feed with Object Detection",
type="numpy",
height=480
)
# Update button for camera
update_btn = gr.Button("Start Camera", variant="primary")
# Set up button click events
forward_btn.click(fn=move_robot, inputs=[gr.Textbox(value="forward", visible=False)], outputs=status_output)
backward_btn.click(fn=move_robot, inputs=[gr.Textbox(value="backward", visible=False)], outputs=status_output)
left_btn.click(fn=move_robot, inputs=[gr.Textbox(value="left", visible=False)], outputs=status_output)
right_btn.click(fn=move_robot, inputs=[gr.Textbox(value="right", visible=False)], outputs=status_output)
stop_btn.click(fn=move_robot, inputs=[gr.Textbox(value="stop", visible=False)], outputs=status_output)
# Set up camera update
update_btn.click(
fn=process_camera_feed,
inputs=None,
outputs=camera_output
)
return interface
# Launch the Gradio interface
if __name__ == "__main__":
robot_interface().launch(share=True, debug=True)