Spaces:
Sleeping
Sleeping
Update obstruction_detector.py
Browse files- obstruction_detector.py +26 -54
obstruction_detector.py
CHANGED
|
@@ -1,54 +1,26 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
smoothed_histogram = cv.filter2D(histogram, -1, kernel)
|
| 28 |
-
|
| 29 |
-
return smoothed_histogram
|
| 30 |
-
|
| 31 |
-
def count_histogram_peaks(self, smoothed_histogram):
|
| 32 |
-
# Find peaks in the smoothed histogram with frequency greater than the threshold
|
| 33 |
-
peaks, _ = find_peaks(smoothed_histogram.flatten(), height=self.threshold)
|
| 34 |
-
return peaks
|
| 35 |
-
|
| 36 |
-
def detect_obstruction(self, img_path):
|
| 37 |
-
# Load the image
|
| 38 |
-
img = cv.imread(img_path)
|
| 39 |
-
|
| 40 |
-
# Preprocess the image
|
| 41 |
-
preprocessed_img = self.preprocess_image(img)
|
| 42 |
-
|
| 43 |
-
# Count the number of peaks in the smoothed histogram above the threshold
|
| 44 |
-
smoothed_histogram = self.plot_histogram(preprocessed_img)
|
| 45 |
-
peaks = self.count_histogram_peaks(smoothed_histogram)
|
| 46 |
-
|
| 47 |
-
# Check if peaks are too close together
|
| 48 |
-
peak_spacing = np.diff(peaks)
|
| 49 |
-
if len(peak_spacing) == 0 or np.all(peak_spacing < 10):
|
| 50 |
-
report = "A imagem NÃO contém obstrução significativa | e NÃO possui múltiplas distribuições de densidade claramente distintas."
|
| 51 |
-
else:
|
| 52 |
-
report = "A imagem contém obstrução significativa | possui múltiplas distribuições de densidade claramente distintas."
|
| 53 |
-
|
| 54 |
-
return report
|
|
|
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|