jake2004 commited on
Commit
5fb87ee
·
verified ·
1 Parent(s): 4232fa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -91
app.py CHANGED
@@ -1,91 +1,158 @@
1
- import sys
2
- from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QTextEdit, QPushButton, QLineEdit, QCheckBox, QHBoxLayout
3
- from PyQt5.QtGui import QFont, QColor, QPalette
4
- from PyQt5.QtCore import Qt
5
- from huggingface_hub import InferenceClient
6
-
7
- class VarunGPTApp(QWidget):
8
- def __init__(self):
9
- super().__init__()
10
- self.initUI()
11
- self.chat_history = ""
12
-
13
- def initUI(self):
14
- self.setWindowTitle("VarunGPT - AI Chatbot")
15
- self.setGeometry(200, 200, 700, 550)
16
-
17
- palette = self.palette()
18
- palette.setColor(QPalette.Window, QColor(20, 20, 20))
19
- self.setPalette(palette)
20
-
21
- layout = QVBoxLayout()
22
-
23
- self.title = QLabel("VarunGPT", self)
24
- self.title.setFont(QFont("Arial", 22, QFont.Bold))
25
- self.title.setStyleSheet("color: #ff9800; text-align: center;")
26
- self.title.setAlignment(Qt.AlignCenter)
27
- layout.addWidget(self.title)
28
-
29
- self.api_key_input = QLineEdit(self)
30
- self.api_key_input.setPlaceholderText("Enter API Key")
31
- self.api_key_input.setEchoMode(QLineEdit.Password)
32
- self.api_key_input.setStyleSheet("background-color: #222; color: white; padding: 8px; border-radius: 5px;")
33
- layout.addWidget(self.api_key_input)
34
-
35
- self.theme_checkbox = QCheckBox("Enable Advanced Reasoning")
36
- self.theme_checkbox.setStyleSheet("color: white;")
37
- layout.addWidget(self.theme_checkbox)
38
-
39
- self.user_input = QLineEdit(self)
40
- self.user_input.setPlaceholderText("Ask anything...")
41
- self.user_input.setStyleSheet("background-color: #222; color: white; padding: 8px; border-radius: 5px;")
42
- layout.addWidget(self.user_input)
43
-
44
- self.send_button = QPushButton("Send", self)
45
- self.send_button.setStyleSheet("background-color: #ff9800; color: white; padding: 10px; border-radius: 5px;")
46
- self.send_button.clicked.connect(self.handleUserInput)
47
- layout.addWidget(self.send_button)
48
-
49
- self.chat_display = QTextEdit(self)
50
- self.chat_display.setReadOnly(True)
51
- self.chat_display.setStyleSheet("background-color: #111; color: white; padding: 10px; border-radius: 5px;")
52
- layout.addWidget(self.chat_display)
53
-
54
- self.setLayout(layout)
55
-
56
- def handleUserInput(self):
57
- api_key = self.api_key_input.text().strip()
58
- if not api_key:
59
- self.chat_display.setText("Error: API Key is required.")
60
- return
61
-
62
- client = InferenceClient(api_key=api_key)
63
-
64
- user_text = self.user_input.text()
65
- if user_text:
66
- self.chat_history += f"You: {user_text}\n"
67
- self.chat_display.setText(self.chat_history)
68
-
69
- ai_response = self.query_huggingface(client, user_text)
70
- self.chat_history += f"VarunGPT: {ai_response}\n"
71
- self.chat_display.setText(self.chat_history)
72
-
73
- self.user_input.clear()
74
-
75
- def query_huggingface(self, client, prompt):
76
- messages = [{"role": "user", "content": prompt}]
77
- try:
78
- completion = client.chat.completions.create(
79
- model="mistralai/Mistral-7B-Instruct-v0.3",
80
- messages=messages,
81
- max_tokens=500
82
- )
83
- return completion["choices"][0]["message"]["content"]
84
- except Exception as e:
85
- return f"Error: {str(e)}"
86
-
87
- if __name__ == "__main__":
88
- app = QApplication(sys.argv)
89
- window = VarunGPTApp()
90
- window.show()
91
- sys.exit(app.exec_())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ from PIL import Image
4
+ import numpy as np
5
+ from ultralytics import YOLO
6
+ import tempfile
7
+ import os
8
+ import supervision as sv
9
+ from streamlit_webrtc import webrtc_streamer, WebRtcMode
10
+ import functools
11
+
12
+ # --- Page Configuration ---
13
+ st.set_page_config(
14
+ page_title="Traffic Lane and Object Detection",
15
+ page_icon=":camera_video:",
16
+ layout="wide",
17
+ initial_sidebar_state="expanded",
18
+ )
19
+
20
+ # --- Sidebar ---
21
+ st.sidebar.header("Traffic Lane and Object Detection Options")
22
+ source_type = st.sidebar.radio("Select Input Source:", ("Image", "Video", "Live Camera Feed")) # Added Live Camera Feed
23
+ confidence_threshold = st.sidebar.slider(
24
+ "Confidence Threshold", min_value=0.0, max_value=1.0, value=0.25, step=0.05
25
+ )
26
+ iou_threshold = st.sidebar.slider(
27
+ "IoU Threshold", min_value=0.0, max_value=1.0, value=0.45, step=0.05, help="Intersection over Union threshold for NMS"
28
+ )
29
+
30
+
31
+ # --- Load YOLO Model ---
32
+ @st.cache_resource
33
+ def load_model():
34
+ model = YOLO("yolov8n.pt") # Load a pretrained YOLOv8n model
35
+ return model
36
+
37
+ model = load_model()
38
+
39
+ # --- Functions ---
40
+ def detect_lanes_and_objects_image(image, model, confidence_threshold, iou_threshold):
41
+ """Runs YOLO on a single image with Supervision."""
42
+ img_np = np.array(image)
43
+ results = model(img_np, conf=confidence_threshold, iou=iou_threshold)
44
+
45
+ detections = sv.Detections.from_ultralytics(results[0])
46
+ annotator = sv.BoxAnnotator(thickness=2, text_thickness=1, text_scale=0.5)
47
+ annotated_frame = annotator.annotate(scene=img_np, detections=detections)
48
+
49
+ return annotated_frame
50
+
51
+
52
+ def detect_lanes_and_objects_video(video_path, model, confidence_threshold, iou_threshold):
53
+ """Runs YOLO on a video file with Supervision."""
54
+ video = cv2.VideoCapture(video_path)
55
+ frame_width = int(video.get(3))
56
+ frame_height = int(video.get(4))
57
+ fps = int(video.get(cv2.CAP_PROP_FPS))
58
+ codec = cv2.VideoWriter_fourcc(*"mp4v")
59
+ output_path = "output.mp4"
60
+ out = cv2.VideoWriter(output_path, codec, fps, (frame_width, frame_height))
61
+ stframe = st.empty()
62
+
63
+ while video.isOpened():
64
+ ret, frame = video.read()
65
+ if not ret:
66
+ break
67
+
68
+ results = model(frame, conf=confidence_threshold, iou=iou_threshold)
69
+
70
+ detections = sv.Detections.from_ultralytics(results[0])
71
+ annotator = sv.BoxAnnotator(thickness=2, text_thickness=1, text_scale=0.5)
72
+ annotated_frame = annotator.annotate(scene=frame, detections=detections)
73
+
74
+ out.write(annotated_frame)
75
+ stframe.image(annotated_frame, channels="BGR", use_column_width=True)
76
+
77
+ video.release()
78
+ out.release()
79
+ cv2.destroyAllWindows()
80
+
81
+ return output_path
82
+
83
+
84
+ def process_frame(frame, model, confidence_threshold, iou_threshold):
85
+ """Process each frame from the webcam with Supervision."""
86
+ img = frame.to_ndarray(format="bgr24")
87
+ results = model(img, conf=confidence_threshold, iou=iou_threshold)
88
+
89
+ detections = sv.Detections.from_ultralytics(results[0])
90
+ annotator = sv.BoxAnnotator(thickness=2, text_thickness=1, text_scale=0.5)
91
+ annotated_frame = annotator.annotate(scene=img, detections=detections)
92
+
93
+ return annotated_frame
94
+
95
+
96
+ # --- Main Application ---
97
+ st.title("Traffic Lane and Object Detection")
98
+
99
+ if source_type == "Image":
100
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
101
+
102
+ if uploaded_file is not None:
103
+ image = Image.open(uploaded_file)
104
+ st.image(image, caption="Uploaded Image", use_column_width=True)
105
+
106
+ if st.button("Run Detection"):
107
+ with st.spinner("Running YOLOv8..."):
108
+ detected_image = detect_lanes_and_objects_image(
109
+ image, model, confidence_threshold, iou_threshold
110
+ )
111
+ st.image(detected_image, caption="Detected Image", use_column_width=True)
112
+
113
+ elif source_type == "Video":
114
+ uploaded_video = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
115
+
116
+ if uploaded_video is not None:
117
+ tfile = tempfile.NamedTemporaryFile(delete=False)
118
+ tfile.write(uploaded_video.read())
119
+ video_path = tfile.name
120
+
121
+ if st.button("Run Detection"):
122
+ with st.spinner("Running YOLOv8 on video..."):
123
+ output_video_path = detect_lanes_and_objects_video(
124
+ video_path, model, confidence_threshold, iou_threshold
125
+ )
126
+ st.video(output_video_path)
127
+ tfile.close()
128
+ os.unlink(video_path)
129
+ os.remove(output_video_path)
130
+
131
+ elif source_type == "Live Camera Feed":
132
+ st.subheader("Live Camera Feed")
133
+
134
+ if model is None:
135
+ st.error("YOLO model is not loaded. Cannot run live feed.")
136
+ st.stop()
137
+
138
+ custom_process_frame = functools.partial(
139
+ process_frame,
140
+ model=model,
141
+ confidence_threshold=confidence_threshold,
142
+ iou_threshold=iou_threshold,
143
+ )
144
+
145
+ webrtc_streamer(
146
+ key="live-feed",
147
+ video_frame_callback=custom_process_frame,
148
+ mode=WebRtcMode.SENDRECV,
149
+ media_stream_constraints={"video": True, "audio": False},
150
+ )
151
+
152
+
153
+ st.markdown("---")
154
+ st.markdown(
155
+ """
156
+ **Note:** This example uses YOLOv8 for object detection. Lane detection is a more complex task and requires additional image processing techniques. This is a simplified demo and will likely not perform well on complex or noisy video.
157
+ """
158
+ )