import streamlit as st
# Set page config as the very first Streamlit command
st.set_page_config(
page_title="SafeLicensing - License Plate Encryption",
page_icon="🛡️",
layout="wide",
initial_sidebar_state="collapsed"
)
import os
import requests
import numpy as np
from PIL import Image, ImageDraw
import io
import random
from ultralytics import YOLO
import time
import cv2
from moviepy.editor import VideoFileClip, ImageSequenceClip
# Configure requests to be more robust
def download_file(url, timeout=15):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(url, headers=headers, timeout=timeout, stream=True)
response.raise_for_status()
return response.content
###############################################################################
# 1. Chaotic Logistic Map Encryption Functions
###############################################################################
def logistic_map(r, x):
return r * x * (1 - x)
def generate_key(seed, n):
"""
Generate a chaotic key (array of size n) using a logistic map and the given seed.
"""
key = []
x = seed
for _ in range(n):
x = logistic_map(3.9, x)
key.append(int(x * 255) % 256) # map float to 0-255
return np.array(key, dtype=np.uint8)
def shuffle_pixels(img_array, seed):
"""
Shuffle the pixels in img_array based on a random sequence seeded by 'seed'.
"""
h, w, c = img_array.shape
num_pixels = h * w
flattened = img_array.reshape(-1, c)
indices = np.arange(num_pixels)
random.seed(seed)
random.shuffle(indices)
shuffled = flattened[indices]
return shuffled.reshape(h, w, c), indices
def encrypt_image(img_array, seed):
"""
Encrypt the given image array using a two-layer XOR + pixel shuffling approach.
"""
h, w, c = img_array.shape
flat_image = img_array.flatten()
# First chaotic key
chaotic_key_1 = generate_key(seed, len(flat_image))
# XOR-based encryption (first layer)
encrypted_flat_1 = [p ^ chaotic_key_1[i] for i, p in enumerate(flat_image)]
encrypted_array_1 = np.array(
encrypted_flat_1, dtype=np.uint8).reshape(h, w, c)
# Shuffle
shuffled_array, _ = shuffle_pixels(encrypted_array_1, seed)
# Second chaotic key
chaotic_key_2 = generate_key(seed * 1.1, len(flat_image))
shuffled_flat = shuffled_array.flatten()
encrypted_flat_2 = [p ^ chaotic_key_2[i]
for i, p in enumerate(shuffled_flat)]
doubly_encrypted_array = np.array(
encrypted_flat_2, dtype=np.uint8).reshape(h, w, c)
return doubly_encrypted_array
###############################################################################
# 2. YOLOv8 License Plate Detection
###############################################################################
@st.cache_resource(show_spinner=False)
def load_model(weights_path: str):
"""
Loads the YOLOv8 model from local .pt weights.
"""
model = YOLO(weights_path)
return model
def detect_license_plates(model, pil_image):
"""
Runs YOLOv8 detection on the PIL image.
Returns:
- image_with_boxes: PIL image with bounding boxes drawn
- bboxes: list of (x1, y1, x2, y2) for detected license plates
"""
np_image = np.array(pil_image)
results = model.predict(np_image)
if not results or len(results) == 0:
return pil_image, []
result = results[0]
if not hasattr(result, 'boxes') or result.boxes is None or len(result.boxes) == 0:
return pil_image, []
bboxes = []
draw = ImageDraw.Draw(pil_image)
for box in result.boxes:
coords = box.xyxy[0].tolist() # [x1, y1, x2, y2]
cls_id = int(box.cls[0].item())
cls_name = model.names.get(cls_id, "Unknown")
if cls_name.lower() == "licenseplate" or cls_id == 0:
x1, y1, x2, y2 = map(int, coords)
bboxes.append((x1, y1, x2, y2))
draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
return pil_image, bboxes
###############################################################################
# 3. Video Processing Functions
###############################################################################
def process_video(video_path, model, key_seed):
"""
Process video frame by frame, detect license plates, and encrypt them.
"""
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
processed_frames = []
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
progress_bar = st.progress(0)
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
_, bboxes = detect_license_plates(model, pil_image)
if bboxes:
for (x1, y1, x2, y2) in bboxes:
plate_region = frame[y1:y2, x1:x2]
encrypted_region = encrypt_image(plate_region, key_seed)
frame[y1:y2, x1:x2] = encrypted_region
processed_frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
frame_count += 1
if total_frames > 0:
progress_bar.progress(frame_count / total_frames)
cap.release()
progress_bar.empty()
return processed_frames, fps, (width, height)
def create_video_from_frames(frames, fps, size, output_path, audio_path=None):
"""
Create a video from processed frames and optionally add audio.
"""
clip = ImageSequenceClip(frames, fps=fps)
if audio_path:
audio = VideoFileClip(audio_path).audio
clip = clip.set_audio(audio)
clip.write_videofile(output_path, codec='libx264')
def inject_styles():
st.markdown(
"""
""",
unsafe_allow_html=True,
)
def render_hero():
st.markdown(
"""
Detect license plates in images and videos, then encrypt only sensitive regions with
chaotic-map masking. Built for fast redaction previews, clean exports, and modern operator flow.
SafeLicensing Space
Choose image or video input, then run targeted protection. Original content stays intact outside detected plate bounds.
Smaller seed changes create different chaotic sequences for plate masking.