HassanDataSci commited on
Commit
7894c61
·
verified ·
1 Parent(s): 730d505

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -38
app.py CHANGED
@@ -1,38 +1,69 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
- from io import BytesIO
4
-
5
- # Load Hugging Face models for question-answering and text-to-speech
6
- @st.cache_resource
7
- def load_qa_pipeline():
8
- return pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
9
-
10
- @st.cache_resource
11
- def load_tts_pipeline():
12
- return pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
13
-
14
- qa_pipeline = load_qa_pipeline()
15
- tts_pipeline = load_tts_pipeline()
16
-
17
- # Streamlit interface
18
- st.title("Virtual Assistant")
19
- st.write("Ask me anything!")
20
-
21
- # User query
22
- user_query = st.text_input("Type your question here:")
23
-
24
- if user_query:
25
- # Generate answer using the QA model
26
- context = "This is the context of the assistant. The assistant will answer general knowledge questions." # Customize context for better QA accuracy
27
- qa_result = qa_pipeline({"question": user_query, "context": context})
28
- answer = qa_result['answer']
29
-
30
- # Display answer as text
31
- st.write(f"Answer: {answer}")
32
-
33
- # Convert answer to audio using TTS model
34
- tts_audio = tts_pipeline(answer, return_tensors="pt").audio
35
-
36
- # Streamlit audio player for TTS output
37
- audio_bytes = BytesIO(tts_audio)
38
- st.audio(audio_bytes, format="audio/wav")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import torch
4
+ from transformers import DetrImageProcessor, DetrForObjectDetection
5
+ from PIL import Image
6
+ import tempfile
7
+ import os
8
+
9
+ # Load the DETR processor and model
10
+ processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
11
+ model = DetrForObjectDetection.from_pretrained("ArrayDice/Vehicle_Detection_Model_Zoom")
12
+
13
+ # Function to process video and detect vehicles
14
+ def detect_vehicles_in_video(video_path):
15
+ # Load video
16
+ cap = cv2.VideoCapture(video_path)
17
+ fps = cap.get(cv2.CAP_PROP_FPS)
18
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
19
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
20
+
21
+ # Temporary output video file
22
+ temp_video = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
23
+ out = cv2.VideoWriter(temp_video.name, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
24
+
25
+ # Process each frame
26
+ while cap.isOpened():
27
+ ret, frame = cap.read()
28
+ if not ret:
29
+ break
30
+
31
+ # Convert frame to PIL for model processing
32
+ pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
33
+
34
+ # Run object detection
35
+ inputs = processor(images=pil_image, return_tensors="pt")
36
+ outputs = model(**inputs)
37
+
38
+ # Set confidence threshold and draw boxes
39
+ for score, label, box in zip(outputs.logits.softmax(-1)[0], outputs.labels[0], outputs.boxes[0]):
40
+ if score.max() >= 0.9: # Adjust confidence threshold as needed
41
+ x_min, y_min, x_max, y_max = map(int, box)
42
+ cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
43
+ label_text = f"Vehicle ({score.max():.2f})"
44
+ cv2.putText(frame, label_text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
45
+
46
+ # Write annotated frame to output video
47
+ out.write(frame)
48
+
49
+ # Release resources
50
+ cap.release()
51
+ out.release()
52
+
53
+ return temp_video.name # Return path to the annotated video
54
+
55
+ # Gradio interface
56
+ def vehicle_detection_gradio(video):
57
+ annotated_video_path = detect_vehicles_in_video(video.name)
58
+ return annotated_video_path
59
+
60
+ # Set up Gradio interface
61
+ iface = gr.Interface(
62
+ fn=vehicle_detection_gradio,
63
+ inputs=gr.Video(type="file", label="Upload a video for vehicle detection"),
64
+ outputs=gr.Video(label="Annotated Video with Detected Vehicles"),
65
+ title="Vehicle Detection Model",
66
+ description="Upload a video, and the model will detect and annotate vehicles in each frame."
67
+ )
68
+
69
+ iface.launch()