Ayushnangia commited on
Commit
42a07d4
1 Parent(s): e1bbb8e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -0
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ import os
4
+ import uuid
5
+ from PIL import Image
6
+ import numpy as np
7
+ import cv2
8
+ is_recording = False
9
+ command_log = []
10
+
11
+ # Placeholder image path or URL
12
+ placeholder_image_path = 'images.png'
13
+ try:
14
+ from jetbot import Robot, Camera, bgr8_to_jpeg
15
+ robot = Robot()
16
+ camera = Camera.instance()
17
+ except ImportError as e:
18
+ print(f"Import error: {e}\nRunning in simulation mode.")
19
+ robot = None
20
+ camera = None
21
+
22
+ def move_forward():
23
+ if robot:
24
+ robot.forward(0.3)
25
+ time.sleep(1)
26
+ robot.stop()
27
+
28
+ def move_backward():
29
+ if robot:
30
+ robot.backward(0.3)
31
+ time.sleep(1)
32
+ robot.stop()
33
+
34
+ def turn_left():
35
+ if robot:
36
+ robot.left(0.3)
37
+ time.sleep(1)
38
+ robot.stop()
39
+
40
+ def turn_right():
41
+ if robot:
42
+ robot.right(0.3)
43
+ time.sleep(1)
44
+ robot.stop()
45
+ def toggle_recording():
46
+ global is_recording
47
+ is_recording = not is_recording
48
+ if not is_recording:
49
+ return "Recording stopped. Ready to replay."
50
+ else:
51
+ command_log.clear()
52
+ return "Recording started. Please execute commands."
53
+
54
+ def record_command(command, *params):
55
+ if is_recording:
56
+ command_log.append((command, params))
57
+
58
+ def replay_commands():
59
+ if not command_log:
60
+ return "No commands recorded."
61
+ for func, params in command_log:
62
+ print(func,params)
63
+ func(*params)
64
+ time.sleep(1) # delay between commands for clarity
65
+ return "Replay finished."
66
+ def stop():
67
+ if robot:
68
+ robot.stop()
69
+
70
+ def set_speed_left(speed):
71
+ if robot:
72
+ robot.left_motor.value = speed
73
+
74
+ def set_speed_right(speed):
75
+ if robot:
76
+ robot.right_motor.value = speed
77
+
78
+ def update_camera():
79
+ if camera:
80
+ # Assuming the camera.value is a numpy array in BGR format
81
+ # Convert from BGR to RGB
82
+ rgb_image = cv2.cvtColor(camera.value, cv2.COLOR_BGR2RGB)
83
+ # Convert numpy array to PIL Image
84
+ pil_image = Image.fromarray(rgb_image)
85
+ return pil_image
86
+ else:
87
+ # Load the placeholder image as a PIL Image
88
+ with open(placeholder_image_path, 'rb') as f:
89
+ pil_image = Image.open(f)
90
+ pil_image.load() # This might be necessary depending on how PIL handles lazy loading
91
+ return pil_image
92
+
93
+
94
+ def move_robot(direction, duration=1.0):
95
+ if robot:
96
+ getattr(robot, direction)(0.3)
97
+ robot.sleep(duration)
98
+ robot.stop()
99
+ record_command(move_robot, direction, duration)
100
+
101
+ def save_snapshot():
102
+ if camera:
103
+ image_path = f"snapshots/{uuid.uuid4()}.jpeg"
104
+ with open(image_path, 'wb') as f:
105
+ f.write(update_camera())
106
+ return image_path
107
+ else:
108
+ return placeholder_image_path
109
+
110
+ # Ensure snapshot directory exists
111
+ os.makedirs('snapshots', exist_ok=True)
112
+
113
+ with gr.Blocks() as demo:
114
+ gr.Markdown("# JetBot Control Panel")
115
+ gr.Markdown("Use the controls below to operate the JetBot. Ensure the area is clear before sending commands to avoid collisions.")
116
+ with gr.Row():
117
+ record_button = gr.Button("🔴 Start/⏹️ Stop Recording")
118
+ replay_button = gr.Button("🔁Replay Commands")
119
+ record_status = gr.Textbox(label="Recording Status", value="Recording not started.")
120
+
121
+ record_button.click(toggle_recording, outputs=record_status)
122
+ replay_button.click(replay_commands, outputs=record_status)
123
+
124
+ with gr.Row():
125
+ left_speed = gr.Slider(-1.0, 1.0, step=0.1, label="Left Motor Speed", value=0.0)
126
+ right_speed = gr.Slider(-1.0, 1.0, step=0.1, label="Right Motor Speed", value=0.0)
127
+
128
+ with gr.Row():
129
+ directions = ['⬆️ Forward', '⬅️ Left', '🛑 Stop', '➡️ Right', '⬇️ Backward']
130
+ for direction in directions:
131
+ gr.Button(direction.title(), elem_id=f"{direction}_button").click(
132
+ fn=lambda x=direction: move_robot(x), inputs=[], outputs=[])
133
+
134
+ left_speed.change(set_speed_left, inputs=[left_speed], outputs=[])
135
+ right_speed.change(set_speed_right, inputs=[right_speed], outputs=[])
136
+
137
+ with gr.Row():
138
+ live_feed = gr.Image(streaming=True,value=update_camera)
139
+ snapshot_result = gr.Image(width=300, height=300, label="Last Snapshot")
140
+ snapshot_button = gr.Button("📸 Take Snapshot", elem_id="snapshot_button").click(save_snapshot, outputs=snapshot_result)
141
+
142
+ gr.Markdown("### Instructions")
143
+ gr.Markdown("1. **Move the Sliders**: Adjust the sliders to change the speed of the left and right motors.")
144
+ gr.Markdown("2. **Press the Buttons**: Use the directional buttons to move the JetBot. Press 'Stop' to halt any movement.")
145
+ gr.Markdown("3. **Camera and Snapshot**: View the live feed and take snapshots using the button.")
146
+
147
+ demo.launch()