pritmanvar-bacancy's picture
Upload 31 files
3c87883 verified
import streamlit as st
import cv2
import io
import numpy as np
from PIL import Image
from pipeline import pipeline
import pandas as pd
import logging
# Configure the logger
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
filename="logs.log",
)
# Create a logger
logger = logging.getLogger("streamlit_app")
code = """
<style>
.block-container{
max-width: 100%;
padding: 50px;
}
# [data-testid="stImage"], .e115fcil2, [data-testid="StyledFullScreenButton"], [data-testid="stFullScreenFrame"].e1vs0wn30, [data-testid="element-container"].e1f1d6gn4.element-container{
# width: fit-content !important;
# }
# [data-testid="stVerticalBlock"].e1f1d6gn2{
# flex-direction: row;
# flex-wrap: wrap;
# }
[data-testid="StyledFullScreenButton"]{
display: none;
}
[data-testid="stVerticalBlockBorderWrapper"], [data-testid="stVerticalBlock"]{
width: 100%;
}
.e115fcil2{
justify-content: center;
margin-top: 20px;
}
</style>
"""
st.html(code)
st.title("Automated Surveillance System")
# Main two columns
col1, col2 = st.columns([5, 5])
container = col2.container(height=800)
# columns to show zoomed images
col3, col4= container.columns([1,1])
# column to take file input and show detected objects.
with col1:
image = st.file_uploader("File upload", label_visibility="hidden")
logger.info("Image is uploaded successfully...")
if image is not None:
image = Image.open(io.BytesIO(image.getvalue()))
image = np.asarray(image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite("images/image.jpg", image)
image = cv2.imread("images/image.jpg")
logger.info("Image is store in image.jpg file...")
results = pipeline(image)
logger.info("getting results of image...")
for result in results:
image = cv2.rectangle(image, result['merged_boundries']['top_left'], result['merged_boundries']['bottom_right'], (255, 0, 0), 1)
st.image(image)
logger.info("image is loaded in app successfully...")
else:
try:
logger.info("default image is used...")
image = cv2.imread("images/default_img.jpg")
results = pipeline(image)
logger.info("getting results of image...")
for result in results:
image = cv2.rectangle(image, result['merged_boundries']['top_left'], result['merged_boundries']['bottom_right'], (255, 0, 0), 1)
st.image(image)
except Exception as e:
logger.error("Something went wrong in Straemlit application inference")
logger.error(e)
# Column to show zoomed images
if results is not None:
with col2:
results_1 = results[:len(results)//2]
results_2 = results[len(results)//2:]
with col4:
for result in results_1:
img = result['zoomed_img']
df = pd.DataFrame(columns=['Object Type', 'Distance', 'Activity'])
actual_width, actual_height = result['merged_boundries']['bottom_right'][0] - result['merged_boundries']['top_left'][0], result['merged_boundries']['bottom_right'][1] - result['merged_boundries']['top_left'][1]
for box in result['actual_boxes']:
top_left = (box['top_left'][0] - result['merged_boundries']['top_left'][0], (box['top_left'][1] - result['merged_boundries']['top_left'][1]))
bottom_right = (box['bottom_right'][0] - result['merged_boundries']['top_left'][0], (box['bottom_right'][1] - result['merged_boundries']['top_left'][1]))
bottom_right = (bottom_right[0]*img.shape[0]//(actual_height), bottom_right[1]*img.shape[1]//(actual_width))
top_left = (top_left[0]*img.shape[0]//(actual_height), top_left[1]*img.shape[1]//(actual_width))
img = cv2.rectangle(img, top_left, bottom_right, (255, 0, 0), 1)
img = cv2.putText(img, "ID: "+str(len(df)), top_left, 1, 1, (255, 255, 255))
df.loc[len(df)] = [box['class'], box['distance'], box['activity']]
st.image(img)
st.table(df)
with col3:
for result in results_2:
img = result['zoomed_img']
df = pd.DataFrame(columns=['Object Type', 'Distance', 'Activity'])
actual_width, actual_height = result['merged_boundries']['bottom_right'][0] - result['merged_boundries']['top_left'][0], result['merged_boundries']['bottom_right'][1] - result['merged_boundries']['top_left'][1]
for box in result['actual_boxes']:
top_left = (box['top_left'][0] - result['merged_boundries']['top_left'][0], (box['top_left'][1] - result['merged_boundries']['top_left'][1]))
bottom_right = (box['bottom_right'][0] - result['merged_boundries']['top_left'][0], (box['bottom_right'][1] - result['merged_boundries']['top_left'][1]))
bottom_right = (bottom_right[0]*img.shape[0]//(actual_height), bottom_right[1]*img.shape[1]//(actual_width))
top_left = (top_left[0]*img.shape[0]//(actual_height), top_left[1]*img.shape[1]//(actual_width))
img = cv2.rectangle(img, top_left, bottom_right, (255, 0, 0), 1)
img = cv2.putText(img, "ID: "+str(len(df)), top_left, 1, 1, (255, 255, 255))
df.loc[len(df)] = [box['class'], box['distance'], box['activity']]
st.image(img)
st.table(df)
logger.info("Results are load successfully...")
logging.info('\n') # Add a blank line
logging.info('\n') # Add a blank line
else:
logger.error("results are not found...")
logging.info('\n') # Add a blank line
logging.info('\n') # Add a blank line