Update app.py
Browse files
app.py
CHANGED
|
@@ -6,21 +6,24 @@ from PIL import Image, ImageDraw
|
|
| 6 |
import io
|
| 7 |
import random
|
| 8 |
|
| 9 |
-
# Attempt to import YOLOv10 from THU-MIG/yolov10
|
| 10 |
try:
|
| 11 |
from ultralytics import YOLOv10
|
| 12 |
except ImportError:
|
| 13 |
-
st.error("Could not import YOLOv10. Please confirm the
|
| 14 |
st.stop()
|
| 15 |
|
| 16 |
-
# ------------------------
|
| 17 |
-
# 1. Chaotic Logistic Map Encryption
|
| 18 |
-
# ------------------------
|
| 19 |
|
| 20 |
def logistic_map(r, x):
|
| 21 |
return r * x * (1 - x)
|
| 22 |
|
| 23 |
def generate_key(seed, n):
|
|
|
|
|
|
|
|
|
|
| 24 |
key = []
|
| 25 |
x = seed
|
| 26 |
for _ in range(n):
|
|
@@ -29,11 +32,14 @@ def generate_key(seed, n):
|
|
| 29 |
return np.array(key, dtype=np.uint8)
|
| 30 |
|
| 31 |
def shuffle_pixels(img_array, seed):
|
|
|
|
|
|
|
|
|
|
| 32 |
h, w, c = img_array.shape
|
| 33 |
num_pixels = h * w
|
| 34 |
flattened = img_array.reshape(-1, c)
|
| 35 |
indices = np.arange(num_pixels)
|
| 36 |
-
|
| 37 |
random.seed(seed)
|
| 38 |
random.shuffle(indices)
|
| 39 |
|
|
@@ -41,7 +47,9 @@ def shuffle_pixels(img_array, seed):
|
|
| 41 |
return shuffled.reshape(h, w, c), indices
|
| 42 |
|
| 43 |
def encrypt_image(img_array, seed):
|
| 44 |
-
"""
|
|
|
|
|
|
|
| 45 |
h, w, c = img_array.shape
|
| 46 |
flat_image = img_array.flatten()
|
| 47 |
|
|
@@ -62,46 +70,58 @@ def encrypt_image(img_array, seed):
|
|
| 62 |
|
| 63 |
return doubly_encrypted_array
|
| 64 |
|
| 65 |
-
# ------------------------
|
| 66 |
# 2. YOLOv10 License Plate Detection
|
| 67 |
-
# ------------------------
|
| 68 |
|
| 69 |
-
@st.cache_data(show_spinner=False)
|
| 70 |
-
def load_model(weights_path):
|
| 71 |
-
"""
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
return model
|
| 74 |
|
| 75 |
def detect_license_plates(model, pil_image):
|
| 76 |
"""
|
| 77 |
Runs YOLOv10 detection on the PIL image.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
Returns:
|
| 79 |
- image_with_boxes: PIL image with bounding boxes drawn
|
| 80 |
-
- bboxes: list of (x1, y1, x2, y2) for detected license plates
|
| 81 |
"""
|
| 82 |
np_image = np.array(pil_image)
|
| 83 |
-
results = model.predict(np_image) #
|
| 84 |
-
|
| 85 |
-
#
|
| 86 |
-
detections = results
|
| 87 |
|
| 88 |
bboxes = []
|
| 89 |
draw = ImageDraw.Draw(pil_image)
|
| 90 |
|
| 91 |
-
for
|
|
|
|
|
|
|
| 92 |
cls_id = int(cls_id)
|
| 93 |
-
#
|
| 94 |
if cls_id == 0:
|
| 95 |
-
x1, y1, x2, y2 = map(int,
|
| 96 |
bboxes.append((x1, y1, x2, y2))
|
| 97 |
-
# Draw bounding box for visualization
|
| 98 |
draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
|
| 99 |
|
| 100 |
return pil_image, bboxes
|
| 101 |
|
| 102 |
-
# ------------------------
|
| 103 |
# 3. Streamlit App
|
| 104 |
-
# ------------------------
|
| 105 |
|
| 106 |
def main():
|
| 107 |
st.title("YOLOv10 + Chaotic Encryption Demo")
|
|
@@ -115,11 +135,11 @@ def main():
|
|
| 115 |
)
|
| 116 |
|
| 117 |
# Model weights path
|
| 118 |
-
|
| 119 |
-
model_path = st.sidebar.text_input("YOLOv10 Weights (.pt)", value=
|
| 120 |
|
| 121 |
if not os.path.isfile(model_path):
|
| 122 |
-
st.warning(f"Model file '{model_path}' not found
|
| 123 |
st.stop()
|
| 124 |
|
| 125 |
with st.spinner("Loading YOLOv10 model..."):
|
|
@@ -129,7 +149,7 @@ def main():
|
|
| 129 |
# Image input
|
| 130 |
st.subheader("Image Input")
|
| 131 |
image_url = st.text_input("Image URL (optional)")
|
| 132 |
-
uploaded_file = st.file_uploader("Or upload an image file", type=["jpg","jpeg","png"])
|
| 133 |
|
| 134 |
# Encryption seed slider
|
| 135 |
key_seed = st.slider("Encryption Key Seed (0 < seed < 1)", 0.001, 0.999, 0.5, step=0.001)
|
|
@@ -171,7 +191,7 @@ def main():
|
|
| 171 |
encrypted_np[y1:y2, x1:x2] = encrypted_region
|
| 172 |
|
| 173 |
encrypted_image = Image.fromarray(encrypted_np)
|
| 174 |
-
|
| 175 |
st.image(encrypted_image, caption="Encrypted Image", use_container_width=True)
|
| 176 |
|
| 177 |
# 4. Download link
|
|
|
|
| 6 |
import io
|
| 7 |
import random
|
| 8 |
|
| 9 |
+
# Attempt to import YOLOv10 from the ultralytics package provided by THU-MIG/yolov10
|
| 10 |
try:
|
| 11 |
from ultralytics import YOLOv10
|
| 12 |
except ImportError:
|
| 13 |
+
st.error("Could not import YOLOv10. Please confirm the THU-MIG/yolov10 installation in requirements.txt.")
|
| 14 |
st.stop()
|
| 15 |
|
| 16 |
+
# -----------------------------------------------------------------------------
|
| 17 |
+
# 1. Chaotic Logistic Map Encryption Functions
|
| 18 |
+
# -----------------------------------------------------------------------------
|
| 19 |
|
| 20 |
def logistic_map(r, x):
|
| 21 |
return r * x * (1 - x)
|
| 22 |
|
| 23 |
def generate_key(seed, n):
|
| 24 |
+
"""
|
| 25 |
+
Generate a chaotic key (array of size n) using a logistic map and the given seed.
|
| 26 |
+
"""
|
| 27 |
key = []
|
| 28 |
x = seed
|
| 29 |
for _ in range(n):
|
|
|
|
| 32 |
return np.array(key, dtype=np.uint8)
|
| 33 |
|
| 34 |
def shuffle_pixels(img_array, seed):
|
| 35 |
+
"""
|
| 36 |
+
Shuffle the pixels in img_array based on a random sequence seeded by 'seed'.
|
| 37 |
+
"""
|
| 38 |
h, w, c = img_array.shape
|
| 39 |
num_pixels = h * w
|
| 40 |
flattened = img_array.reshape(-1, c)
|
| 41 |
indices = np.arange(num_pixels)
|
| 42 |
+
|
| 43 |
random.seed(seed)
|
| 44 |
random.shuffle(indices)
|
| 45 |
|
|
|
|
| 47 |
return shuffled.reshape(h, w, c), indices
|
| 48 |
|
| 49 |
def encrypt_image(img_array, seed):
|
| 50 |
+
"""
|
| 51 |
+
Encrypt the given image array using a two-layer XOR + pixel shuffling approach.
|
| 52 |
+
"""
|
| 53 |
h, w, c = img_array.shape
|
| 54 |
flat_image = img_array.flatten()
|
| 55 |
|
|
|
|
| 70 |
|
| 71 |
return doubly_encrypted_array
|
| 72 |
|
| 73 |
+
# -----------------------------------------------------------------------------
|
| 74 |
# 2. YOLOv10 License Plate Detection
|
| 75 |
+
# -----------------------------------------------------------------------------
|
| 76 |
|
| 77 |
+
@st.cache_data(show_spinner=False)
|
| 78 |
+
def load_model(weights_path: str):
|
| 79 |
+
"""
|
| 80 |
+
Loads the YOLOv10 model from local .pt weights.
|
| 81 |
+
Make sure the path is correct and the .pt file is in your Space if it's custom.
|
| 82 |
+
"""
|
| 83 |
+
model = YOLOv10(weights_path)
|
| 84 |
return model
|
| 85 |
|
| 86 |
def detect_license_plates(model, pil_image):
|
| 87 |
"""
|
| 88 |
Runs YOLOv10 detection on the PIL image.
|
| 89 |
+
|
| 90 |
+
According to the THU-MIG/yolov10 usage, 'model.predict(np_image)'
|
| 91 |
+
typically returns a list of bounding boxes in the format:
|
| 92 |
+
[ [x1, y1, x2, y2, conf, class_id], [x1, y1, x2, y2, conf, class_id], ... ]
|
| 93 |
+
|
| 94 |
+
We assume class_id == 0 corresponds to a license plate.
|
| 95 |
+
|
| 96 |
Returns:
|
| 97 |
- image_with_boxes: PIL image with bounding boxes drawn
|
| 98 |
+
- bboxes: list of (x1, y1, x2, y2) for any detected license plates
|
| 99 |
"""
|
| 100 |
np_image = np.array(pil_image)
|
| 101 |
+
results = model.predict(np_image) # Usually a list of detections for each image
|
| 102 |
+
# If there's only one image, results is something like [[x1, y1, x2, y2, conf, cls], ...]
|
| 103 |
+
# So we grab the first list:
|
| 104 |
+
detections = results[0] if len(results) > 0 else []
|
| 105 |
|
| 106 |
bboxes = []
|
| 107 |
draw = ImageDraw.Draw(pil_image)
|
| 108 |
|
| 109 |
+
for det in detections:
|
| 110 |
+
# Unpack [x1, y1, x2, y2, conf, cls_id]
|
| 111 |
+
x1, y1, x2, y2, conf, cls_id = det
|
| 112 |
cls_id = int(cls_id)
|
| 113 |
+
# e.g., if your license plate is class 0
|
| 114 |
if cls_id == 0:
|
| 115 |
+
x1, y1, x2, y2 = map(int, (x1, y1, x2, y2))
|
| 116 |
bboxes.append((x1, y1, x2, y2))
|
| 117 |
+
# Draw bounding box (optional, for visualization)
|
| 118 |
draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
|
| 119 |
|
| 120 |
return pil_image, bboxes
|
| 121 |
|
| 122 |
+
# -----------------------------------------------------------------------------
|
| 123 |
# 3. Streamlit App
|
| 124 |
+
# -----------------------------------------------------------------------------
|
| 125 |
|
| 126 |
def main():
|
| 127 |
st.title("YOLOv10 + Chaotic Encryption Demo")
|
|
|
|
| 135 |
)
|
| 136 |
|
| 137 |
# Model weights path
|
| 138 |
+
default_model_path = "best.pt" # Adjust if your model file has a different name
|
| 139 |
+
model_path = st.sidebar.text_input("YOLOv10 Weights (.pt)", value=default_model_path)
|
| 140 |
|
| 141 |
if not os.path.isfile(model_path):
|
| 142 |
+
st.warning(f"Model file '{model_path}' not found. Upload or provide a correct path.")
|
| 143 |
st.stop()
|
| 144 |
|
| 145 |
with st.spinner("Loading YOLOv10 model..."):
|
|
|
|
| 149 |
# Image input
|
| 150 |
st.subheader("Image Input")
|
| 151 |
image_url = st.text_input("Image URL (optional)")
|
| 152 |
+
uploaded_file = st.file_uploader("Or upload an image file", type=["jpg", "jpeg", "png"])
|
| 153 |
|
| 154 |
# Encryption seed slider
|
| 155 |
key_seed = st.slider("Encryption Key Seed (0 < seed < 1)", 0.001, 0.999, 0.5, step=0.001)
|
|
|
|
| 191 |
encrypted_np[y1:y2, x1:x2] = encrypted_region
|
| 192 |
|
| 193 |
encrypted_image = Image.fromarray(encrypted_np)
|
| 194 |
+
|
| 195 |
st.image(encrypted_image, caption="Encrypted Image", use_container_width=True)
|
| 196 |
|
| 197 |
# 4. Download link
|