exorcist123 commited on
Commit
5823599
·
1 Parent(s): 508dd84

try streamlit

Browse files
Files changed (3) hide show
  1. crowd_counter/__init__.py +9 -13
  2. main.py +88 -0
  3. requirements.txt +12 -11
crowd_counter/__init__.py CHANGED
@@ -69,19 +69,15 @@ class CrowdCounter:
69
  ) -> tuple[any, Image.Image, torch.Tensor]:
70
 
71
  ori_width, ori_height = img_raw.size
72
- max_dimension = 384
73
-
74
- if ori_width > max_dimension or ori_height > max_dimension:
75
- scale_factor = max_dimension / max(ori_width, ori_height)
76
- new_width = int(ori_width * scale_factor)
77
- new_height = int(ori_height * scale_factor)
78
- new_width = new_width // 128 * 128
79
- new_height = new_height // 128 * 128
80
- img_resized = img_raw.resize((new_width, new_height), Image.LANCZOS)
81
- else:
82
- img_resized = img_raw
83
- new_width = ori_width
84
- new_height = ori_height
85
 
86
  print(new_width, new_height)
87
  # pre-proccessing
 
69
  ) -> tuple[any, Image.Image, torch.Tensor]:
70
 
71
  ori_width, ori_height = img_raw.size
72
+ max_dimension = 512
73
+
74
+ scale_factor = max_dimension / max(ori_width, ori_height)
75
+ new_width = int(ori_width * scale_factor)
76
+ new_height = int(ori_height * scale_factor)
77
+ print(new_width, new_height)
78
+ new_width = new_width // 128 * 128
79
+ new_height = new_height // 128 * 128
80
+ img_resized = img_raw.resize((new_width, new_height), Image.LANCZOS)
 
 
 
 
81
 
82
  print(new_width, new_height)
83
  # pre-proccessing
main.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from crowd_counter import CrowdCounter
3
+ from PIL import Image
4
+ import cv2
5
+ import numpy as np
6
+
7
+ # Initialize the crowd counter engine
8
+ crowd_counter_engine = CrowdCounter()
9
+
10
+ # Function to predict crowd count and generate prediction image
11
+ def predict(input_image):
12
+ input_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
13
+ response = crowd_counter_engine.inference(input_image)
14
+ crowd_count, pred_img = response
15
+ pred_img_rgb = cv2.cvtColor(pred_img, cv2.COLOR_BGR2RGB)
16
+ return pred_img_rgb, crowd_count
17
+
18
+ # Streamlit UI layout
19
+ st.markdown("<h1 style='text-align: center; margin-bottom: 0.5rem;'>Crowd Counter Demo</h1>", unsafe_allow_html=True)
20
+ st.markdown("<h3 style='text-align: center; margin-bottom: 1rem;'>A Demo of Proposal Point Prediction for Crowd Counting</h3>", unsafe_allow_html=True)
21
+
22
+ # Radio button to choose between example images and uploading an image
23
+ option = st.radio('Choose an image source:', ('Use Example Image', 'Upload Your Own Image'))
24
+
25
+ # Example images
26
+ example_images = {
27
+ "Example 1": "images/img-1.jpg",
28
+ "Example 2": "images/img-2.jpg",
29
+ "Example 3": "images/img-3.jpg",
30
+ }
31
+
32
+ # Handling the selection
33
+ if option == 'Use Example Image':
34
+ # Select box for example images
35
+ example_selection = st.selectbox('Choose an example image:', list(example_images.keys()))
36
+
37
+ if example_selection:
38
+ example_image_path = example_images[example_selection]
39
+ example_image = Image.open(example_image_path)
40
+ st.image(example_image, caption='Selected Example Image', use_column_width=True)
41
+
42
+ # Convert the PIL Image to an array for processing
43
+ example_image_array = np.array(example_image)
44
+
45
+ with st.spinner("Thinking..."):
46
+ # Predict the crowd count and proposal points for the example image
47
+ pred_img, crowd_count = predict(example_image_array)
48
+
49
+ # Display the prediction results
50
+ st.success(f"Predicted Count: {crowd_count}")
51
+ st.image(pred_img, caption='Proposal Points Prediction', use_column_width=True)
52
+
53
+
54
+ else: # 'Upload Your Own Image' is selected
55
+ uploaded_file = st.file_uploader("Or upload an image:", type=['jpg', 'jpeg', 'png'])
56
+
57
+ if uploaded_file is not None:
58
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
59
+ user_img = cv2.imdecode(file_bytes, 1)
60
+
61
+ st.image(user_img, caption='Uploaded Image', use_column_width=True)
62
+
63
+ with st.spinner("Thinking..."):
64
+ # Predict the crowd count and proposal points for the uploaded image
65
+ pred_img, crowd_count = predict(user_img)
66
+
67
+ # Display the prediction results
68
+ st.success(f"Predicted Count: {crowd_count}")
69
+ st.image(pred_img, caption='Proposal Points Prediction', use_column_width=True)
70
+
71
+ st.markdown('---')
72
+ footer = """
73
+ <style>
74
+ .footer {
75
+ font-family: Arial, sans-serif;
76
+ font-size: small;
77
+ text-align: center;
78
+ padding: 10px;
79
+ margin-top: 20px;
80
+ }
81
+ </style>
82
+ <div class="footer">
83
+ &copy; 2023 <a href="https://www.ristek.cs.ui.ac.id/" target="_blank">RISTEK Fasilkom UI</a> <br>All rights reserved<br>
84
+ Powered by <a href="https://github.com/TencentYoutuResearch/CrowdCounting-P2PNet/" target="_blank">P2PNet</a>
85
+ </div>
86
+ """
87
+
88
+ st.markdown(footer, unsafe_allow_html=True)
requirements.txt CHANGED
@@ -1,11 +1,12 @@
1
- torch
2
- torchvision
3
- tensorboardX
4
- easydict
5
- pandas
6
- numpy
7
- scipy
8
- matplotlib
9
- Pillow
10
- opencv-python
11
- gradio==3.50
 
 
1
+ torch
2
+ torchvision
3
+ tensorboardX
4
+ easydict
5
+ pandas
6
+ numpy
7
+ scipy
8
+ matplotlib
9
+ Pillow
10
+ opencv-python
11
+ gradio==3.50
12
+ # streamlit