Fzina commited on
Commit
c5eb4fd
·
verified ·
1 Parent(s): f5a83ab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import torch
4
+ import numpy as np
5
+ import gspread
6
+ from oauth2client.service_account import ServiceAccountCredentials
7
+ import requests
8
+ import os
9
+
10
+ api_key = os.getenv("WEATHER_API_KEY")
11
+
12
+ # Load Pre-trained YOLOv5 Model from Hugging Face
13
+ model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # YOLOv5 small model
14
+
15
+ # Google Sheets Setup
16
+ def setup_google_sheets(sheet_name):
17
+ scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
18
+ creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
19
+ client = gspread.authorize(creds)
20
+ sheet = client.open(sheet_name).sheet1
21
+ return sheet
22
+
23
+ sheet = setup_google_sheets("TrafficManagementData")
24
+
25
+ # Analyze Traffic with YOLOv5
26
+ def analyze_traffic(image):
27
+ # Perform vehicle detection
28
+ results = model(image)
29
+ detected_objects = results.pandas().xyxy[0]
30
+ vehicle_count = len(detected_objects[detected_objects['name'].isin(['car', 'truck', 'bus', 'motorbike'])])
31
+
32
+ # Predict congestion level based on vehicle count
33
+ congestion_level = "High" if vehicle_count > 150 else "Medium" if vehicle_count > 75 else "Low"
34
+ flow_rate = "Slow" if congestion_level == "High" else "Moderate" if congestion_level == "Medium" else "Smooth"
35
+ return vehicle_count, congestion_level, flow_rate
36
+
37
+ # Optimize Signal Timing
38
+ def optimize_signal(vehicle_count, congestion_level):
39
+ if congestion_level == "High":
40
+ return "Green for 40s, Red for 20s"
41
+ elif congestion_level == "Medium":
42
+ return "Green for 30s, Red for 30s"
43
+ return "Green for 20s, Red for 40s"
44
+
45
+ # Get Weather Impact on Traffic
46
+ def get_weather_impact(api_key):
47
+ url = f"http://api.openweathermap.org/data/2.5/weather?q=New+York&appid={api_key}"
48
+ response = requests.get(url)
49
+ if response.status_code == 200:
50
+ data = response.json()
51
+ weather = data['weather'][0]['description']
52
+ if "rain" in weather or "snow" in weather:
53
+ return "Traffic is likely to be slower due to weather conditions."
54
+ return "Weather is clear, no major impact on traffic."
55
+ return "Error fetching weather data."
56
+
57
+ # Process Traffic Image
58
+ def process_traffic_image(image):
59
+ # Analyze traffic using YOLO
60
+ vehicle_count, congestion_level, flow_rate = analyze_traffic(image)
61
+
62
+ # Optimize signal based on analysis
63
+ signal_timing = optimize_signal(vehicle_count, congestion_level)
64
+
65
+ # Get weather impact
66
+ weather_impact = get_weather_impact(WEATHER_API_KEY)
67
+
68
+ # Log data into Google Sheets
69
+ sheet.append_row([vehicle_count, congestion_level, flow_rate, signal_timing, weather_impact])
70
+
71
+ # Provide detailed results
72
+ return (f"Detected Vehicles: {vehicle_count}\n"
73
+ f"Congestion Level: {congestion_level}\n"
74
+ f"Traffic Flow: {flow_rate}\n"
75
+ f"Signal Timing Suggestion: {signal_timing}\n"
76
+ f"Weather Impact: {weather_impact}")
77
+
78
+ # Gradio Interface
79
+ interface = gr.Interface(
80
+ fn=process_traffic_image,
81
+ inputs=gr.Image(type="numpy", label="Upload Traffic Image"),
82
+ outputs="text",
83
+ title="Enhanced AI-Powered Smart Traffic Management System",
84
+ description="Upload a traffic image to analyze congestion, optimize signal timing, and predict traffic flow."
85
+ )
86
+
87
+ if __name__ == "__main__":
88
+ interface.launch()