Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -196,14 +196,11 @@ def process_video_and_save(uploaded_file):
|
|
196 |
license_plate_text = model_ocr.chat(processor, temp_image_path, ocr_type='ocr')
|
197 |
filtered_text = filter_license_plate_text(license_plate_text)
|
198 |
# Check if the license plate is already detected and saved
|
199 |
-
if filtered_text:
|
200 |
-
# Get the email from the database
|
201 |
-
email = get_vehicle_information(filtered_text, lane_violation, helmet_violation, violation_image_path, "Riyadh")
|
202 |
-
|
203 |
# Add the license plate and its violations to the violations dictionary
|
204 |
if filtered_text not in violations_dict:
|
205 |
violations_dict[filtered_text] = violation_type #{"1234AB":[no_Helmet,In_red_Lane]}
|
206 |
-
send_email(filtered_text, violation_image_path, ', '.join(violation_type)
|
207 |
else:
|
208 |
# Update the violations for the license plate if new ones are found
|
209 |
current_violations = set(violations_dict[filtered_text]) # no helmet
|
@@ -213,7 +210,7 @@ def process_video_and_save(uploaded_file):
|
|
213 |
# If new violations are found, update and send email
|
214 |
if updated_violations != violations_dict[filtered_text]:
|
215 |
violations_dict[filtered_text] = updated_violations
|
216 |
-
send_email(filtered_text, violation_image_path, ', '.join(updated_violations)
|
217 |
|
218 |
# Draw OCR text (English and Arabic) on the original frame
|
219 |
arabic_text = convert_to_arabic(filtered_text)
|
@@ -320,177 +317,6 @@ def draw_text_pil(img, text, position, font_path, font_size, color):
|
|
320 |
img_np = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR)
|
321 |
return img_np
|
322 |
|
323 |
-
import sqlite3
|
324 |
-
from datetime import datetime
|
325 |
-
from sqlalchemy import create_engine
|
326 |
-
from sqlalchemy.orm import sessionmaker
|
327 |
-
from sqlalchemy.ext.declarative import declarative_base
|
328 |
-
from sqlalchemy import Column, String, Integer, Boolean
|
329 |
-
|
330 |
-
def get_vehicle_information(detected_license_plate, lane_violation, no_helmet, image_link, city):
|
331 |
-
# Get current date and time
|
332 |
-
current_datetime = datetime.now()
|
333 |
-
current_date = current_datetime.strftime('%Y-%m-%d')
|
334 |
-
current_time = current_datetime.strftime('%H:%M:%S')
|
335 |
-
|
336 |
-
# Connect to SQLite database (motorbike_detections.db)
|
337 |
-
try:
|
338 |
-
motorbike_conn = sqlite3.connect('motorbike_detections.db')
|
339 |
-
motorbike_cursor = motorbike_conn.cursor()
|
340 |
-
|
341 |
-
# Check if the detection already exists
|
342 |
-
check_query = '''
|
343 |
-
SELECT DetectionID FROM MotorbikeDetections
|
344 |
-
WHERE LicensePlate = ? AND Date = ? AND Time = ? AND ImageLink = ?
|
345 |
-
'''
|
346 |
-
motorbike_cursor.execute(check_query, (detected_license_plate, current_date, current_time, image_link))
|
347 |
-
existing_detection = motorbike_cursor.fetchone()
|
348 |
-
|
349 |
-
if existing_detection:
|
350 |
-
print(f"Detection for license plate {detected_license_plate} at {current_date} {current_time} already exists.")
|
351 |
-
else:
|
352 |
-
# Insert the new detection record
|
353 |
-
insert_query = '''
|
354 |
-
INSERT INTO MotorbikeDetections (Date, Time, City, LicensePlate, LaneViolation, NoHelmet, ImageLink)
|
355 |
-
VALUES (?, ?, ?, ?, ?, ?, ?)
|
356 |
-
'''
|
357 |
-
motorbike_cursor.execute(insert_query, (
|
358 |
-
current_date,
|
359 |
-
current_time,
|
360 |
-
city,
|
361 |
-
detected_license_plate,
|
362 |
-
int(lane_violation), # Convert boolean to integer (1 or 0)
|
363 |
-
int(no_helmet), # Convert boolean to integer (1 or 0)
|
364 |
-
image_link
|
365 |
-
))
|
366 |
-
|
367 |
-
# Commit the transaction
|
368 |
-
motorbike_conn.commit()
|
369 |
-
print(f"Detection data for license plate {detected_license_plate} inserted successfully.")
|
370 |
-
|
371 |
-
except sqlite3.IntegrityError as e:
|
372 |
-
print(f"Integrity Error: {e}. This detection may already exist.")
|
373 |
-
except sqlite3.Error as e:
|
374 |
-
print(f"An error occurred while inserting detection data: {e}")
|
375 |
-
motorbike_conn.rollback()
|
376 |
-
finally:
|
377 |
-
# Close the motorbike detections database connection
|
378 |
-
motorbike_conn.close()
|
379 |
-
|
380 |
-
# Retrieve email from 'vehicle_information.db'
|
381 |
-
try:
|
382 |
-
# Create an engine and session for SQLAlchemy
|
383 |
-
engine = create_engine('sqlite:///vehicle_information.db')
|
384 |
-
Session = sessionmaker(bind=engine)
|
385 |
-
session = Session()
|
386 |
-
|
387 |
-
# Query the VehicleInformation table for the detected license plate
|
388 |
-
vehicle_info = session.query(VehicleInformation).filter_by(license_plate=detected_license_plate).first()
|
389 |
-
|
390 |
-
if vehicle_info:
|
391 |
-
print(f"Email found for license plate {detected_license_plate}: {vehicle_info.email}")
|
392 |
-
return vehicle_info.email
|
393 |
-
else:
|
394 |
-
print(f"No vehicle information found for license plate {detected_license_plate}.")
|
395 |
-
return None
|
396 |
-
|
397 |
-
except Exception as e:
|
398 |
-
print(f"An error occurred while retrieving vehicle information: {e}")
|
399 |
-
return None
|
400 |
-
|
401 |
-
finally:
|
402 |
-
# Close the SQLAlchemy session
|
403 |
-
session.close()
|
404 |
-
|
405 |
-
import sqlite3
|
406 |
-
from datetime import datetime
|
407 |
-
|
408 |
-
def setup_motorbike_detections_db():
|
409 |
-
# Connect to SQLite database (or create it if it doesn't exist)
|
410 |
-
conn = sqlite3.connect('motorbike_detections.db')
|
411 |
-
cursor = conn.cursor()
|
412 |
-
|
413 |
-
# Drop the old table if it exists, useful for restructuring
|
414 |
-
cursor.execute('DROP TABLE IF EXISTS MotorbikeDetections')
|
415 |
-
|
416 |
-
# Create the new table with a unique constraint on LicensePlate, Date, Time, and ImageLink
|
417 |
-
cursor.execute('''
|
418 |
-
CREATE TABLE IF NOT EXISTS MotorbikeDetections (
|
419 |
-
DetectionID INTEGER PRIMARY KEY AUTOINCREMENT,
|
420 |
-
Date DATE NOT NULL,
|
421 |
-
Time TIME NOT NULL,
|
422 |
-
City VARCHAR(100),
|
423 |
-
LicensePlate VARCHAR(100),
|
424 |
-
LaneViolation BOOLEAN NOT NULL,
|
425 |
-
NoHelmet BOOLEAN NOT NULL,
|
426 |
-
ImageLink VARCHAR(255),
|
427 |
-
UNIQUE(LicensePlate, Date, Time, ImageLink)
|
428 |
-
)
|
429 |
-
''')
|
430 |
-
|
431 |
-
# Commit changes
|
432 |
-
conn.commit()
|
433 |
-
|
434 |
-
# Close the connection
|
435 |
-
conn.close()
|
436 |
-
|
437 |
-
|
438 |
-
from sqlalchemy import create_engine, Column, String, Integer
|
439 |
-
from sqlalchemy.ext.declarative import declarative_base
|
440 |
-
from sqlalchemy.orm import sessionmaker
|
441 |
-
|
442 |
-
# Define the base for model creation
|
443 |
-
Base = declarative_base()
|
444 |
-
|
445 |
-
# Define the VehicleInformation table using SQLAlchemy ORM
|
446 |
-
class VehicleInformation(Base):
|
447 |
-
__tablename__ = 'VehicleInformation'
|
448 |
-
|
449 |
-
id = Column(Integer, primary_key=True, autoincrement=True)
|
450 |
-
license_plate = Column(String(100), unique=True, nullable=False)
|
451 |
-
email = Column(String(255), nullable=False)
|
452 |
-
phone_number = Column(String(15), nullable=False)
|
453 |
-
driver_id = Column(String(50), nullable=False) # Government ID or identity number
|
454 |
-
|
455 |
-
def setup_database():
|
456 |
-
# Create an SQLite database (this could be any database like PostgreSQL, MySQL, etc.)
|
457 |
-
engine = create_engine('sqlite:///vehicle_information.db')
|
458 |
-
|
459 |
-
# Drop existing tables and recreate them (for ensuring clean database on rerun)
|
460 |
-
Base.metadata.drop_all(engine)
|
461 |
-
Base.metadata.create_all(engine)
|
462 |
-
|
463 |
-
# Create a session to interact with the database
|
464 |
-
Session = sessionmaker(bind=engine)
|
465 |
-
session = Session()
|
466 |
-
|
467 |
-
# Insert some dummy data into the VehicleInformation table
|
468 |
-
vehicle_data_1 = VehicleInformation(
|
469 |
-
license_plate='1234 AB',
|
470 |
-
email='[email protected]',
|
471 |
-
phone_number='0559947203',
|
472 |
-
driver_id='ID1110000000'
|
473 |
-
)
|
474 |
-
|
475 |
-
vehicle_data_2 = VehicleInformation(
|
476 |
-
license_plate='3321 AR',
|
477 |
-
email='[email protected]',
|
478 |
-
phone_number='0539003545',
|
479 |
-
driver_id='ID2220000000'
|
480 |
-
)
|
481 |
-
|
482 |
-
# Add records to the session
|
483 |
-
session.add(vehicle_data_1)
|
484 |
-
session.add(vehicle_data_2)
|
485 |
-
|
486 |
-
# Commit the records to the database
|
487 |
-
session.commit()
|
488 |
-
|
489 |
-
# Query the table to confirm data
|
490 |
-
vehicles = session.query(VehicleInformation).all()
|
491 |
-
|
492 |
-
# Close the session
|
493 |
-
session.close()
|
494 |
|
495 |
# Streamlit app main function
|
496 |
def main():
|
|
|
196 |
license_plate_text = model_ocr.chat(processor, temp_image_path, ocr_type='ocr')
|
197 |
filtered_text = filter_license_plate_text(license_plate_text)
|
198 |
# Check if the license plate is already detected and saved
|
199 |
+
if filtered_text:
|
|
|
|
|
|
|
200 |
# Add the license plate and its violations to the violations dictionary
|
201 |
if filtered_text not in violations_dict:
|
202 |
violations_dict[filtered_text] = violation_type #{"1234AB":[no_Helmet,In_red_Lane]}
|
203 |
+
send_email(filtered_text, violation_image_path, ', '.join(violation_type))
|
204 |
else:
|
205 |
# Update the violations for the license plate if new ones are found
|
206 |
current_violations = set(violations_dict[filtered_text]) # no helmet
|
|
|
210 |
# If new violations are found, update and send email
|
211 |
if updated_violations != violations_dict[filtered_text]:
|
212 |
violations_dict[filtered_text] = updated_violations
|
213 |
+
send_email(filtered_text, violation_image_path, ', '.join(updated_violations))
|
214 |
|
215 |
# Draw OCR text (English and Arabic) on the original frame
|
216 |
arabic_text = convert_to_arabic(filtered_text)
|
|
|
317 |
img_np = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR)
|
318 |
return img_np
|
319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
|
321 |
# Streamlit app main function
|
322 |
def main():
|