DHEIVER commited on
Commit
900259d
·
1 Parent(s): 0eeaa55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -42
app.py CHANGED
@@ -1,48 +1,26 @@
1
  import gradio as gr
2
- import tempfile
3
  from PIL import Image
4
- import numpy as np
5
- from scipy.signal import find_peaks
6
- import cv2 as cv # Add this import
7
  from obstruction_detector import ObstructionDetector
8
 
9
- # Crie um detector de obstrução
10
  detector = ObstructionDetector()
11
 
12
- # Function to preprocess the image and return the report, preprocessed image, and histogram
13
- # Function to preprocess the image and return the report, preprocessed image, and histogram
14
- def preprocess_image(image):
15
- import cv2 as cv # Import OpenCV here
16
-
17
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_image:
18
- image.save(temp_image.name, "PNG")
19
- img_path = temp_image.name
20
- report = detector.detect_obstruction(img_path)
21
-
22
- # Load the image again for display
23
- img = cv.imread(img_path)
24
-
25
- # Preprocess the image
26
- preprocessed_img = detector.preprocess_image(img)
27
-
28
- # Calculate and smooth the histogram
29
- smoothed_histogram = detector.plot_histogram(preprocessed_img)
30
-
31
- # Convert the preprocessed image to a PIL image for display
32
- preprocessed_img_pil = Image.fromarray(cv.cvtColor(preprocessed_img, cv.COLOR_BGR2RGB))
33
-
34
- # Convert the histogram to a grayscale image and scale it to [0, 255]
35
- histogram_min = smoothed_histogram.min()
36
- histogram_max = smoothed_histogram.max()
37
- scaled_histogram = ((smoothed_histogram - histogram_min) / (histogram_max - histogram_min) * 255).astype(np.uint8)
38
- histogram_img_pil = Image.fromarray(scaled_histogram)
39
-
40
- return report, preprocessed_img_pil, histogram_img_pil # Return the histogram as an image
41
-
42
- # Create the Gradio interface
43
- iface = gr.Interface(fn=preprocess_image,
44
- inputs=gr.inputs.Image(type="pil", label="Carregar Imagem"),
45
- outputs=["text", "image", "image"]) # Output the histogram as an image
46
-
47
- # Launch the interface
48
- iface.launch()
 
1
  import gradio as gr
 
2
  from PIL import Image
3
+
4
+ # Import the ObstructionDetector class from your module
 
5
  from obstruction_detector import ObstructionDetector
6
 
7
+ # Create an instance of ObstructionDetector
8
  detector = ObstructionDetector()
9
 
10
+ # Define a Gradio function to process the image and return the report
11
+ def process_image(image):
12
+ # Convert Gradio image data to a PIL image
13
+ pil_image = Image.fromarray(image.astype('uint8'), 'RGB')
14
+
15
+ # Call the detect_obstruction method of the ObstructionDetector
16
+ report = detector.detect_obstruction_from_pil_image(pil_image)
17
+
18
+ return report
19
+
20
+ # Define the Gradio interface
21
+ iface = gr.Interface(fn=process_image,
22
+ inputs=gr.inputs.Image(shape=(224, 224)), # Adjust shape as needed
23
+ outputs="text")
24
+
25
+ # Launch the Gradio interface
26
+ iface.launch()