Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import edge_tts
|
| 4 |
+
import tempfile
|
| 5 |
+
import numpy as np
|
| 6 |
+
from torchvision.models.detection import fasterrcnn_resnet50_fpn
|
| 7 |
+
import torchvision.transforms as transforms
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from huggingface_hub import InferenceClient
|
| 10 |
+
|
| 11 |
+
class YoloDetector:
|
| 12 |
+
def __init__(self, weights_path, cfg_path, names_path):
|
| 13 |
+
self.net = cv2.dnn.readNet(weights_path, cfg_path)
|
| 14 |
+
self.classes = []
|
| 15 |
+
with open(names_path, "r") as f:
|
| 16 |
+
self.classes = [line.strip() for line in f.readlines()]
|
| 17 |
+
self.layer_names = self.net.getLayerNames()
|
| 18 |
+
self.output_layers = [self.layer_names[i[0] - 1] for i in self.net.getUnconnectedOutLayers()]
|
| 19 |
+
|
| 20 |
+
def detect_objects(self, frame):
|
| 21 |
+
height, width, channels = frame.shape
|
| 22 |
+
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
|
| 23 |
+
self.net.setInput(blob)
|
| 24 |
+
outs = self.net.forward(self.output_layers)
|
| 25 |
+
|
| 26 |
+
class_ids = []
|
| 27 |
+
confidences = []
|
| 28 |
+
boxes = []
|
| 29 |
+
for out in outs:
|
| 30 |
+
for detection in out:
|
| 31 |
+
scores = detection[5:]
|
| 32 |
+
class_id = np.argmax(scores)
|
| 33 |
+
confidence = scores[class_id]
|
| 34 |
+
if confidence > 0.5:
|
| 35 |
+
center_x = int(detection[0] * width)
|
| 36 |
+
center_y = int(detection[1] * height)
|
| 37 |
+
w = int(detection[2] * width)
|
| 38 |
+
h = int(detection[3] * height)
|
| 39 |
+
x = int(center_x - w / 2)
|
| 40 |
+
y = int(center_y - h / 2)
|
| 41 |
+
boxes.append([x, y, w, h])
|
| 42 |
+
confidences.append(float(confidence))
|
| 43 |
+
class_ids.append(class_id)
|
| 44 |
+
|
| 45 |
+
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
| 46 |
+
font = cv2.FONT_HERSHEY_PLAIN
|
| 47 |
+
for i in range(len(boxes)):
|
| 48 |
+
if i in indexes:
|
| 49 |
+
x, y, w, h = boxes[i]
|
| 50 |
+
label = str(self.classes[class_ids[i]])
|
| 51 |
+
color = (0, 255, 0)
|
| 52 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
|
| 53 |
+
cv2.putText(frame, label, (x, y + 30), font, 3, color, 2)
|
| 54 |
+
|
| 55 |
+
return frame
|
| 56 |
+
|
| 57 |
+
class JarvisModels:
|
| 58 |
+
def __init__(self):
|
| 59 |
+
self.client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 60 |
+
self.detector = YoloDetector("yolov3.weights", "yolov3.cfg", "coco.names")
|
| 61 |
+
|
| 62 |
+
async def generate_model1(self, prompt):
|
| 63 |
+
generate_kwargs = dict(
|
| 64 |
+
temperature=0.6,
|
| 65 |
+
max_new_tokens=256,
|
| 66 |
+
top_p=0.95,
|
| 67 |
+
repetition_penalty=1,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
seed=42,
|
| 70 |
+
)
|
| 71 |
+
formatted_prompt = system_instructions1 + prompt + "[JARVIS]"
|
| 72 |
+
stream = self.client1.text_generation(
|
| 73 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
| 74 |
+
output = ""
|
| 75 |
+
for response in stream:
|
| 76 |
+
output += response.token.text
|
| 77 |
+
|
| 78 |
+
communicate = edge_tts.Communicate(output)
|
| 79 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 80 |
+
tmp_path = tmp_file.name
|
| 81 |
+
communicate.save(tmp_path)
|
| 82 |
+
return tmp_path
|
| 83 |
+
|
| 84 |
+
class FasterRCNNDetector:
|
| 85 |
+
def __init__(self):
|
| 86 |
+
self.model = fasterrcnn_resnet50_fpn(pretrained=True)
|
| 87 |
+
self.model.eval()
|
| 88 |
+
self.classes = [
|
| 89 |
+
"__background__", "person", "bicycle", "car", "motorcycle", "airplane", "bus",
|
| 90 |
+
"train", "truck", "boat", "traffic light", "fire hydrant", "N/A", "stop sign",
|
| 91 |
+
"parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
|
| 92 |
+
"elephant", "bear", "zebra", "giraffe", "N/A", "backpack", "umbrella", "N/A", "N/A",
|
| 93 |
+
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
|
| 94 |
+
"kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket",
|
| 95 |
+
"bottle", "N/A", "wine glass", "cup", "fork", "knife", "spoon", "bowl",
|
| 96 |
+
"banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza",
|
| 97 |
+
"donut", "cake", "chair", "couch", "potted plant", "bed", "N/A", "dining table",
|
| 98 |
+
"N/A", "N/A", "toilet", "N/A", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
|
| 99 |
+
"microwave", "oven", "toaster", "sink", "refrigerator", "N/A", "book",
|
| 100 |
+
"clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
def detect_objects(self, image):
|
| 104 |
+
image_pil = Image.fromarray(image)
|
| 105 |
+
transform = transforms.Compose([transforms.ToTensor()])
|
| 106 |
+
image_tensor = transform(image_pil).unsqueeze(0)
|
| 107 |
+
|
| 108 |
+
with torch.no_grad():
|
| 109 |
+
prediction = self.model(image_tensor)
|
| 110 |
+
|
| 111 |
+
boxes = prediction[0]['boxes']
|
| 112 |
+
labels = prediction[0]['labels']
|
| 113 |
+
scores = prediction[0]['scores']
|
| 114 |
+
|
| 115 |
+
for box, label, score in zip(boxes, labels, scores):
|
| 116 |
+
box = [int(i) for i in box]
|
| 117 |
+
cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
|
| 118 |
+
cv2.putText(image, self.classes[label], (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
|
| 119 |
+
|
| 120 |
+
return image
|
| 121 |
+
|
| 122 |
+
def generate_response(frame):
|
| 123 |
+
jarvis = JarvisModels()
|
| 124 |
+
detector = FasterRCNNDetector()
|
| 125 |
+
frame_with_boxes = jarvis.detector.detect_objects(frame)
|
| 126 |
+
cv2.imwrite("temp.jpg", frame_with_boxes)
|
| 127 |
+
communicate = edge_tts.Communicate("Objects detected!")
|
| 128 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 129 |
+
tmp_path = tmp_file.name
|
| 130 |
+
communicate.save(tmp_path)
|
| 131 |
+
return tmp_path
|
| 132 |
+
|
| 133 |
+
iface = gr.Webcam(gr.Video(label="Webcam", parameters=["fps=30"], is_streaming=True), preprocess=generate_response, postprocess=FasterRCNNDetector().detect_objects, show_loading=False)
|
| 134 |
+
gr.Interface(fn=iface, layout="vertical", capture_session=True).launch()
|