2023-11-17 14:22:28 +01:00
|
|
|
"""Base state for the app."""
|
|
|
|
|
|
|
|
import reflex as rx
|
2024-01-09 02:15:41 +01:00
|
|
|
import os
|
2024-01-16 14:12:10 +01:00
|
|
|
import stat
|
2024-01-16 19:21:51 +01:00
|
|
|
#from blurme import styles
|
2023-12-12 21:27:48 +01:00
|
|
|
import asyncio
|
2024-01-09 02:15:41 +01:00
|
|
|
from graphics.image_modification import blur_boxes_on_image
|
2024-01-15 21:25:58 +01:00
|
|
|
from graphics.image_modification import show_image_with_boxes
|
2024-01-09 02:15:41 +01:00
|
|
|
from ml.element_detection import BoundBox, detect
|
|
|
|
|
2023-11-17 14:22:28 +01:00
|
|
|
class State(rx.State):
|
2023-12-12 21:27:48 +01:00
|
|
|
"""The app state."""
|
|
|
|
|
|
|
|
# The images to show.
|
|
|
|
img: list[str] = []
|
2024-01-16 02:39:24 +01:00
|
|
|
|
|
|
|
show: bool = False
|
|
|
|
def toggle_show(self):
|
|
|
|
self.show = not self.show
|
2024-01-16 14:12:10 +01:00
|
|
|
|
|
|
|
show_extension_alert: bool = False
|
|
|
|
def toggle_extension_alert(self):
|
|
|
|
self.show_extension_alert = not self.show_extension_alert
|
2024-01-08 18:59:34 +01:00
|
|
|
|
|
|
|
def change(self):
|
|
|
|
self.show = not (self.show)
|
|
|
|
|
2023-12-19 13:39:26 +01:00
|
|
|
|
2023-12-12 21:27:48 +01:00
|
|
|
async def handle_upload(self, files: list[rx.UploadFile]):
|
|
|
|
"""Handle the upload of file(s).
|
|
|
|
Args:
|
|
|
|
files: The uploaded files.
|
|
|
|
"""
|
2024-01-16 01:07:20 +01:00
|
|
|
max_files_limit = 5
|
2024-01-16 14:12:10 +01:00
|
|
|
acceptable_extensions = {"png", "jpg", "jpeg", "webp", "tif", "tiff"}
|
|
|
|
|
2024-01-16 01:07:20 +01:00
|
|
|
if len(self.img) + len(files) > max_files_limit:
|
2024-01-16 02:39:24 +01:00
|
|
|
self.toggle_show()
|
2024-01-16 01:07:20 +01:00
|
|
|
return
|
|
|
|
|
2024-01-16 02:39:24 +01:00
|
|
|
for file in files:
|
2024-01-08 22:08:23 +01:00
|
|
|
upload_data = await file.read()
|
|
|
|
outfile = rx.get_asset_path(file.filename)
|
2023-12-12 21:28:17 +01:00
|
|
|
|
2024-01-16 14:12:10 +01:00
|
|
|
extension = file.filename.split(".")[-1]
|
|
|
|
|
|
|
|
if extension.lower() not in acceptable_extensions:
|
|
|
|
self.toggle_extension_alert()
|
|
|
|
return
|
|
|
|
|
2023-12-12 21:27:48 +01:00
|
|
|
# Save the file.
|
2024-01-08 22:08:23 +01:00
|
|
|
with open(outfile, "wb") as file_object:
|
2023-12-12 21:27:48 +01:00
|
|
|
file_object.write(upload_data)
|
2023-12-19 13:39:26 +01:00
|
|
|
|
2024-01-16 14:12:10 +01:00
|
|
|
# Update the img var.
|
2024-01-16 02:39:24 +01:00
|
|
|
self.img.append(file.filename)
|
|
|
|
|
2023-12-12 21:27:48 +01:00
|
|
|
self.img = self.img
|
2024-01-16 02:39:24 +01:00
|
|
|
|
2024-01-16 01:07:20 +01:00
|
|
|
|
2023-12-19 13:39:26 +01:00
|
|
|
def delete_image(self, img_name: str):
|
|
|
|
if img_name in self.img:
|
|
|
|
self.img.remove(img_name)
|
|
|
|
self.img=self.img
|
2024-01-16 02:39:24 +01:00
|
|
|
|
2023-12-19 16:13:56 +01:00
|
|
|
def download_image(self, img_name):
|
|
|
|
print(self.img)
|
|
|
|
return rx.download(url=f'/{img_name}', filename=img_name)
|
|
|
|
|
2024-01-15 21:25:58 +01:00
|
|
|
async def image_analysis(self, img_name: str):
|
|
|
|
if img_name in self.img:
|
2024-01-16 19:21:51 +01:00
|
|
|
stripped_image_name = str(img_name).lstrip("analysis-").lstrip("anonim-")
|
2024-01-15 21:25:58 +01:00
|
|
|
image_path = rx.get_asset_path(stripped_image_name)
|
|
|
|
#timestamp = datetime.now().strftime("%Y%m%d%H%M%S%f")
|
|
|
|
new_image_name = f"analysis-{stripped_image_name}"
|
|
|
|
new_image_path = rx.get_asset_path(new_image_name)
|
|
|
|
show_image_with_boxes(image_path, detect(image_path), new_image_path)
|
|
|
|
upload_file = rx.UploadFile(file=open(new_image_path, 'rb'), filename=new_image_name)
|
|
|
|
self.delete_image(img_name)
|
|
|
|
await self.handle_upload([upload_file])
|
|
|
|
self.img=self.img
|
|
|
|
|
2024-01-09 02:15:41 +01:00
|
|
|
async def image_anonymization(self, img_name: str):
|
|
|
|
if img_name in self.img:
|
2024-01-16 19:21:51 +01:00
|
|
|
stripped_image_name = str(img_name).lstrip("analysis-").lstrip("anonim-")
|
|
|
|
image_path = rx.get_asset_path(stripped_image_name)
|
|
|
|
#timestamp = datetime.now().strftime("%Y%m%d%H%M%S%f")
|
|
|
|
new_image_name = f"anonim-{stripped_image_name}"
|
|
|
|
new_image_path = rx.get_asset_path(new_image_name)
|
2024-01-09 02:15:41 +01:00
|
|
|
blur_boxes_on_image(image_path, detect(image_path), new_image_path)
|
2024-01-16 19:21:51 +01:00
|
|
|
upload_file = rx.UploadFile(file=open(new_image_path, 'rb'), filename=new_image_name)
|
2024-01-09 02:15:41 +01:00
|
|
|
self.delete_image(img_name)
|
|
|
|
await self.handle_upload([upload_file])
|
2024-01-16 19:21:51 +01:00
|
|
|
self.img=self.img
|