ahmedxeno commited on
Commit
ac2c4c0
·
verified ·
1 Parent(s): 31fdb6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -13
app.py CHANGED
@@ -1,21 +1,65 @@
1
- import gradio as gr
2
  import cv2
3
  import numpy as np
4
- from ultralytics import YOLO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- model = YOLO('best.pt')
 
 
 
7
 
8
- def predict_image(img):
9
-
10
- model = YOLO('best.pt')
11
- result = model.predict(img,conf=0.2)
12
- res_plotted = result[0].plot()
13
- #cv2.imshow( res_plotted)
14
- return res_plotted
15
 
 
 
 
 
 
 
 
16
 
 
 
 
17
 
18
- image = gr.inputs.Image()
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- label = gr.outputs.Label('ok')
21
- gr.Interface(fn=predict_image, inputs=image, outputs=image).launch(debug='True')
 
 
1
  import cv2
2
  import numpy as np
3
+ import gradio as gr
4
+ import pywt
5
+ from skimage import exposure
6
+
7
+ def musica_enhancement(img):
8
+ # Convert from RGB to grayscale if needed
9
+ if len(img.shape) == 3:
10
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
11
+
12
+ # Normalize to [0, 1]
13
+ img_norm = img.astype(np.float32) / 255.0
14
+
15
+ # 1. Wavelet Decomposition
16
+ coeffs = pywt.wavedec2(img_norm, 'bior1.3', level=3)
17
+ cA3, (cH3, cV3, cD3), (cH2, cV2, cD2), (cH1, cV1, cD1) = coeffs
18
+
19
+ # 2. Frequency Band Processing
20
+ cD1 = pywt.threshold(cD1, 0.05*np.max(cD1), mode='soft')
21
+ cD2 = pywt.threshold(cD2, 0.07*np.max(cD2), mode='soft')
22
+ cH1 = cH1 * 1.2
23
+ cV1 = cV1 * 1.2
24
+
25
+ # 3. Reconstruction with Clipping
26
+ coeffs_enhanced = [cA3, (cH3, cV3, cD3), (cH2, cV2, cD2), (cH1, cV1, cD1)]
27
+ img_recon = pywt.waverec2(coeffs_enhanced, 'bior1.3')
28
+ img_recon = np.clip(img_recon, 0, 1)
29
 
30
+ # 4. Adaptive CLAHE
31
+ entropy = -np.sum(img_recon * np.log2(img_recon + 1e-7))
32
+ clip_limit = 0.02 if entropy > 7 else 0.05
33
+ img_clahe = exposure.equalize_adapthist(img_recon, clip_limit=clip_limit, kernel_size=64)
34
 
35
+ # 5. Gamma Correction
36
+ p5, p95 = np.percentile(img_clahe, (5, 95))
37
+ gamma = 0.7 if (p95 - p5) < 0.3 else 0.9
38
+ img_gamma = exposure.adjust_gamma(img_clahe, gamma=gamma)
 
 
 
39
 
40
+ # 6. Edge Enhancement
41
+ img_gamma_8bit = (img_gamma * 255).astype(np.uint8)
42
+ img_bgr = cv2.cvtColor(img_gamma_8bit, cv2.COLOR_GRAY2BGR)
43
+ img_sharp = cv2.detailEnhance(img_bgr, sigma_s=12, sigma_r=0.15)
44
+ img_sharp = cv2.cvtColor(img_sharp, cv2.COLOR_BGR2RGB)
45
+
46
+ return img_sharp
47
 
48
+ def process_image(input_img):
49
+ enhanced_img = musica_enhancement(input_img)
50
+ return input_img, enhanced_img
51
 
52
+ # Gradio Interface
53
+ demo = gr.Interface(
54
+ fn=process_image,
55
+ inputs=gr.Image(label="Upload X-ray"),
56
+ outputs=[
57
+ gr.Image(label="Original Image"),
58
+ gr.Image(label="Enhanced Image")
59
+ ],
60
+ examples=[["./sample_xray.jpg"]],
61
+ title="X-ray Enhancement (MUSICA®-Style)",
62
+ description="Medical X-ray enhancement using multi-scale wavelet processing and adaptive contrast adjustment"
63
+ )
64
 
65
+ demo.launch()