File size: 3,917 Bytes
6781c17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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)