Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,94 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
st.dataframe(df)
|
| 46 |
-
|
| 47 |
-
over_24 = df[df['Duration (Hours)'] > 24]
|
| 48 |
-
|
| 49 |
-
if not over_24.empty:
|
| 50 |
-
st.subheader("β οΈ Vehicles > 24 Hours")
|
| 51 |
-
st.dataframe(over_24)
|
| 52 |
-
|
| 53 |
-
if st.button("Send Email Alerts"):
|
| 54 |
-
for _, row in over_24.iterrows():
|
| 55 |
-
success = send_email_alert(row['Vehicle Number'], row['Duration (Hours)'])
|
| 56 |
-
if success:
|
| 57 |
-
st.success(f"Alert sent for {row['Vehicle Number']}")
|
| 58 |
-
else:
|
| 59 |
-
st.info("β
No vehicles exceeding 24-hour duration.")
|
| 60 |
-
except Exception as e:
|
| 61 |
-
st.error(f"Failed to process file: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import cv2
|
| 4 |
+
import easyocr
|
| 5 |
+
import tempfile
|
| 6 |
+
import requests
|
| 7 |
+
import os
|
| 8 |
from datetime import datetime
|
| 9 |
+
from urllib.parse import urlparse, parse_qs
|
| 10 |
+
|
| 11 |
+
# CONFIG
|
| 12 |
+
API_KEY = st.secrets.get("GDRIVE_API_KEY", "your_google_api_key")
|
| 13 |
+
FOLDER_ID = st.text_input("π Google Drive Folder ID", "")
|
| 14 |
+
|
| 15 |
+
LOG_FILE = "vehicle_log.csv"
|
| 16 |
+
FRAME_SKIP = 30
|
| 17 |
+
|
| 18 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
| 19 |
+
|
| 20 |
+
# Ensure log file
|
| 21 |
+
if not os.path.exists(LOG_FILE):
|
| 22 |
+
pd.DataFrame(columns=["Vehicle Number", "Timestamp", "Video File"]).to_csv(LOG_FILE, index=False)
|
| 23 |
+
|
| 24 |
+
def list_drive_files(folder_id):
|
| 25 |
+
url = f"https://www.googleapis.com/drive/v3/files?q='{folder_id}'+in+parents+and+(mimeType='video/mp4'+or+mimeType='video/avi')&key={API_KEY}&fields=files(id,name)"
|
| 26 |
+
resp = requests.get(url)
|
| 27 |
+
if resp.status_code != 200:
|
| 28 |
+
st.error(f"Drive API Error: {resp.text}")
|
| 29 |
+
return []
|
| 30 |
+
return resp.json().get("files", [])
|
| 31 |
+
|
| 32 |
+
def download_drive_video(file_id):
|
| 33 |
+
download_url = f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media&key={API_KEY}"
|
| 34 |
+
resp = requests.get(download_url, stream=True)
|
| 35 |
+
if resp.status_code != 200:
|
| 36 |
+
return None
|
| 37 |
+
temp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 38 |
+
for chunk in resp.iter_content(chunk_size=8192):
|
| 39 |
+
if chunk:
|
| 40 |
+
temp.write(chunk)
|
| 41 |
+
temp.close()
|
| 42 |
+
return temp.name
|
| 43 |
+
|
| 44 |
+
def process_video(video_path, video_name, logged_plates):
|
| 45 |
+
cap = cv2.VideoCapture(video_path)
|
| 46 |
+
frame_num = 0
|
| 47 |
+
new_logs = []
|
| 48 |
+
|
| 49 |
+
while cap.isOpened():
|
| 50 |
+
ret, frame = cap.read()
|
| 51 |
+
if not ret:
|
| 52 |
+
break
|
| 53 |
+
if frame_num % FRAME_SKIP == 0:
|
| 54 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 55 |
+
results = reader.readtext(gray)
|
| 56 |
+
for (_, text, _) in results:
|
| 57 |
+
text = text.replace(" ", "").upper()
|
| 58 |
+
if len(text) >= 6 and any(char.isdigit() for char in text) and text not in logged_plates:
|
| 59 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 60 |
+
new_logs.append([text, timestamp, video_name])
|
| 61 |
+
logged_plates.add(text)
|
| 62 |
+
st.success(f"Detected: {text} at {timestamp}")
|
| 63 |
+
frame_num += 1
|
| 64 |
+
cap.release()
|
| 65 |
+
return new_logs
|
| 66 |
+
|
| 67 |
+
st.title("π Vehicle Number Detection from Google Drive CCTV")
|
| 68 |
+
|
| 69 |
+
if FOLDER_ID:
|
| 70 |
+
files = list_drive_files(FOLDER_ID)
|
| 71 |
+
if not files:
|
| 72 |
+
st.warning("No video files found in the folder.")
|
| 73 |
+
else:
|
| 74 |
+
log_df = pd.read_csv(LOG_FILE)
|
| 75 |
+
logged_plates = set(log_df["Vehicle Number"])
|
| 76 |
+
all_new_logs = []
|
| 77 |
+
|
| 78 |
+
for file in files:
|
| 79 |
+
st.info(f"Processing: {file['name']}")
|
| 80 |
+
local_path = download_drive_video(file['id'])
|
| 81 |
+
if local_path:
|
| 82 |
+
new_logs = process_video(local_path, file['name'], logged_plates)
|
| 83 |
+
all_new_logs.extend(new_logs)
|
| 84 |
+
os.remove(local_path)
|
| 85 |
+
|
| 86 |
+
if all_new_logs:
|
| 87 |
+
pd.DataFrame(all_new_logs, columns=["Vehicle Number", "Timestamp", "Video File"]).to_csv(LOG_FILE, mode='a', index=False, header=False)
|
| 88 |
+
st.success("β
Logs updated.")
|
| 89 |
else:
|
| 90 |
+
st.info("No new vehicles detected.")
|
| 91 |
+
|
| 92 |
+
if os.path.exists(LOG_FILE):
|
| 93 |
+
st.subheader("π Vehicle Entry Log")
|
| 94 |
+
st.dataframe(pd.read_csv(LOG_FILE))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|