asmaa105 commited on
Commit
4c1b733
·
verified ·
1 Parent(s): 7ea01fd

Upload 4 files

Browse files
Files changed (4) hide show
  1. best.pt +3 -0
  2. packages.txt +1 -0
  3. requirements.txt +11 -0
  4. streamlitTEST.py +84 -0
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a3899264fa4f4f1dd5b79b2bd748597aa7bb400e5ec57244b5dace63bb451c45
3
+ size 52028609
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ libgl1
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Flask==3.0.3
2
+ ipython==8.12.3
3
+ numpy==2.0.0
4
+ opencv_python==4.9.0.80
5
+ opencv_python_headless==4.10.0.84
6
+ pandas==2.2.2
7
+ Pillow==10.4.0
8
+ Requests==2.32.3
9
+ roboflow==1.1.34
10
+ streamlit==1.36.0
11
+ ultralytics==8.0.196
streamlitTEST.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from ultralytics import YOLO
3
+ from PIL import Image, ImageDraw
4
+ import tempfile
5
+
6
+ def greeting(name):
7
+ return f"Hello from module2, {name}!"
8
+
9
+ # Function to process the image, classify it, and crop if clear
10
+ def process_image(file_path):
11
+ model = YOLO("best.pt", "v8")
12
+
13
+ # Predict with the model
14
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_image:
15
+ temp_image.close()
16
+ Image.open(file_path).save(temp_image.name)
17
+ results = model.predict(source=temp_image.name, conf=0.4, save=True)
18
+
19
+ blur_conf_threshold = 0.5
20
+ clear_conf_threshold = 0.9
21
+
22
+ # Initialize flags
23
+ is_blur = False
24
+ is_clear = False
25
+ cropped_image = None
26
+
27
+ # Process results
28
+ for result in results[0].boxes:
29
+ confidence = result.conf[0].item() # Extract the confidence score
30
+ if blur_conf_threshold <= confidence <= clear_conf_threshold:
31
+ is_blur = True
32
+ elif confidence > clear_conf_threshold:
33
+ is_clear = True
34
+ box = result.xyxy[0].tolist() # Extract bounding box coordinates
35
+ cropped_image = crop_image(file_path, box)
36
+
37
+ # Return classification and cropped image
38
+ if is_blur:
39
+ return 'The image is blurry. Please reupload the image again!', None
40
+ elif is_clear:
41
+ return 'The image is clear', cropped_image
42
+ else:
43
+ s=("Not Detected! The image is uncertain. Please reupload the image again!")
44
+ return s, None
45
+
46
+ # Function to crop the image based on bounding box
47
+ def crop_image(file_path, box):
48
+ image = Image.open(file_path)
49
+ cropped_image = image.crop(box)
50
+ return cropped_image
51
+
52
+ def greeting(name):
53
+ return f"Hello module2, {name}!"
54
+
55
+ # Streamlit app
56
+ def main():
57
+ st.title('Welcome to my AI project')
58
+ st.title('Document Detection')
59
+ st.text('This is a web app to:\n1- Detect documents\n2- Classify if document is clear or blurry\n3- Crop the document image!')
60
+
61
+ # File uploader
62
+ uploaded_file = st.file_uploader('Upload your image here:', type=['png', 'jpg', 'jpeg'])
63
+
64
+
65
+
66
+ if uploaded_file is not None:
67
+ # Display the uploaded image
68
+ image = Image.open(uploaded_file)
69
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
70
+ st.success("Photo uploaded successfully!")
71
+
72
+ # Process the image and get classification and cropped image
73
+ classification, cropped_image = process_image(uploaded_file)
74
+
75
+ # Display classification result
76
+ st.write('Classification result:', classification)
77
+
78
+ # Display cropped image if classification is clear
79
+ if cropped_image is not None:
80
+ st.image(cropped_image, caption='Cropped Document Image.', use_column_width=True)
81
+
82
+ if __name__ == "__main__":
83
+ main()
84
+