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|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import cv2
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
import base64
|
| 7 |
+
import fitz
|
| 8 |
+
os.chdir(r'D:\WebApplication_YOLO_AD_detectsystem\The Trail Image Folder')
|
| 9 |
+
# Define a function to apply custom CSS
|
| 10 |
+
pathimage = r"D:\WebApplication_YOLO_AD_detectsystem\BackgroundImage\rm314-adj-10.jpg"
|
| 11 |
+
|
| 12 |
+
def get_base64(bin_file):
|
| 13 |
+
with open(bin_file, 'rb') as f:
|
| 14 |
+
data = f.read()
|
| 15 |
+
return base64.b64encode(data).decode()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def set_background(png_file):
|
| 19 |
+
bin_str = get_base64(png_file)
|
| 20 |
+
page_bg_img = '''
|
| 21 |
+
<style>
|
| 22 |
+
.stApp {
|
| 23 |
+
background-image: url("data:image/png;base64,%s");
|
| 24 |
+
background-size: cover;
|
| 25 |
+
}
|
| 26 |
+
</style>
|
| 27 |
+
''' % bin_str
|
| 28 |
+
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 29 |
+
|
| 30 |
+
set_background(pathimage)
|
| 31 |
+
|
| 32 |
+
st.title(':orange[Advertisement Detection Web App]')
|
| 33 |
+
|
| 34 |
+
# Define custom CSS to style the text area and text color
|
| 35 |
+
# Define custom CSS to style the text area with a black background and white text color
|
| 36 |
+
custom_css = """
|
| 37 |
+
<style>
|
| 38 |
+
/* Add a border to the text area */
|
| 39 |
+
.custom-text-area {
|
| 40 |
+
border: 1px solid #000; /* You can adjust the border properties as needed */
|
| 41 |
+
border-radius: 5px;
|
| 42 |
+
padding: 10px;
|
| 43 |
+
background-color: black; /* Black background color */
|
| 44 |
+
color: white; /* White text color */
|
| 45 |
+
}
|
| 46 |
+
</style>
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
# Apply the custom CSS
|
| 50 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
| 51 |
+
|
| 52 |
+
# Instructions:
|
| 53 |
+
multi = """ Instructions:--
|
| 54 |
+
|
| 55 |
+
1. The Model Trained with English & Tamil NewsPapers.
|
| 56 |
+
|
| 57 |
+
2. Use any type of News paper wether PDF or Image file, the Model will automaticall Detect adds.
|
| 58 |
+
|
| 59 |
+
3. The model will take at a time whole newspaper but it is recommended to upload single page or image. It is very useful for us to count and verify the published ads.
|
| 60 |
+
|
| 61 |
+
4. The Model accuracy is around 80%.
|
| 62 |
+
|
| 63 |
+
5. To convert PDF to images and to get single pages use the Below website upload the news paper and download the single pages.
|
| 64 |
+
|
| 65 |
+
'http://172.17.4.69:8501'
|
| 66 |
+
"""
|
| 67 |
+
st.markdown(multi)
|
| 68 |
+
|
| 69 |
+
# Function to convert PDF to images
|
| 70 |
+
def pdf_to_img(uploaded_file, img_path_prefix):
|
| 71 |
+
# Save the uploaded file
|
| 72 |
+
with open(uploaded_file.name, "wb") as f:
|
| 73 |
+
f.write(uploaded_file.getvalue())
|
| 74 |
+
|
| 75 |
+
file_extension = os.path.splitext(uploaded_file.name)[1].lower()
|
| 76 |
+
|
| 77 |
+
if file_extension == ".pdf":
|
| 78 |
+
pdf = fitz.open(uploaded_file.name)
|
| 79 |
+
image_paths = []
|
| 80 |
+
for page_number in range(pdf.page_count):
|
| 81 |
+
page = pdf[page_number]
|
| 82 |
+
# Convert the page to a pixmap
|
| 83 |
+
pixmap = page.get_pixmap()
|
| 84 |
+
|
| 85 |
+
# Convert the Pixmap to a Pillow Image
|
| 86 |
+
img = Image.frombytes("RGB", [pixmap.width, pixmap.height], pixmap.samples)
|
| 87 |
+
|
| 88 |
+
# Save the image as JPEG
|
| 89 |
+
image_path = f"{img_path_prefix}_page_{page_number + 1}.jpeg"
|
| 90 |
+
img.save(image_path)
|
| 91 |
+
image_paths.append(image_path)
|
| 92 |
+
|
| 93 |
+
pdf.close()
|
| 94 |
+
return image_paths
|
| 95 |
+
elif file_extension == ".jpeg" or file_extension == ".jpg":
|
| 96 |
+
# If the uploaded file is already an image, return its path
|
| 97 |
+
image_path = f"{img_path_prefix}_uploaded_image.jpeg"
|
| 98 |
+
with open(image_path, "wb") as f:
|
| 99 |
+
f.write(uploaded_file.getvalue())
|
| 100 |
+
return [image_path]
|
| 101 |
+
else:
|
| 102 |
+
st.error("Unsupported file format. Please upload a PDF or JPEG image.")
|
| 103 |
+
return []
|
| 104 |
+
|
| 105 |
+
# Function to perform object detection
|
| 106 |
+
def perform_object_detection(image_path):
|
| 107 |
+
# Load the YOLO model
|
| 108 |
+
model = YOLO(r"D:\ADS_Project_Deployment\Models\Detection Models\best31_1000_epochs.pt")
|
| 109 |
+
|
| 110 |
+
# Load and preprocess the image
|
| 111 |
+
img = cv2.imread(image_path)
|
| 112 |
+
|
| 113 |
+
results = model(img)
|
| 114 |
+
|
| 115 |
+
detections = [] # Store tuples of bounding box and confidence
|
| 116 |
+
# Access the detected objects and their properties
|
| 117 |
+
if isinstance(results, list):
|
| 118 |
+
for res in results:
|
| 119 |
+
if res.boxes is not None:
|
| 120 |
+
for det, confidence in zip(res.boxes.xyxy, res.boxes.conf):
|
| 121 |
+
x1, y1, x2, y2 = map(int, det[:4])
|
| 122 |
+
confidence_value = round(confidence.item(), 2)
|
| 123 |
+
detections.append(((x1, y1, x2, y2), confidence_value))
|
| 124 |
+
else:
|
| 125 |
+
print("No detections found in the current element.")
|
| 126 |
+
else:
|
| 127 |
+
print("No results found.")
|
| 128 |
+
|
| 129 |
+
return detections
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# Main function
|
| 133 |
+
def main():
|
| 134 |
+
# st.title("Advertisement Detection Web App")
|
| 135 |
+
|
| 136 |
+
# File upload
|
| 137 |
+
uploaded_file = st.file_uploader("Choose a file", type=["pdf", "jpeg", "jpg"])
|
| 138 |
+
|
| 139 |
+
if uploaded_file is not None:
|
| 140 |
+
# Convert PDF to images or use the uploaded image directly
|
| 141 |
+
image_paths = pdf_to_img(uploaded_file, "uploaded_image")
|
| 142 |
+
|
| 143 |
+
if image_paths:
|
| 144 |
+
# Perform object detection for each image
|
| 145 |
+
for idx, image_path in enumerate(image_paths):
|
| 146 |
+
st.image(image_path, caption=f"Page {idx + 1}", use_column_width=True)
|
| 147 |
+
st.write(f"### Detected Advertisements - Page {idx + 1}")
|
| 148 |
+
detections = perform_object_detection(image_path)
|
| 149 |
+
|
| 150 |
+
# Iterate through the detections and extract the detected images
|
| 151 |
+
for i, (detection, confidence) in enumerate(detections):
|
| 152 |
+
x1, y1, x2, y2 = detection
|
| 153 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # Convert the coordinates to integers
|
| 154 |
+
# Crop the image using the bounding box coordinates
|
| 155 |
+
detected_image = cv2.imread(image_path)[y1:y2, x1:x2]
|
| 156 |
+
# Display the detected image
|
| 157 |
+
st.image(detected_image, caption=f"Detected Image {i + 1}", use_column_width=True)
|
| 158 |
+
st.write(f"Confidence: {confidence}")
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
main()
|