karthikmn commited on
Commit
5934d13
·
verified ·
1 Parent(s): ab1f9b0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ def process_image(image, capture_from_webcam=False):
7
+ """
8
+ Process an uploaded or captured image: convert it to grayscale.
9
+ """
10
+ if capture_from_webcam:
11
+ # Decode the webcam image
12
+ image = cv2.imdecode(np.frombuffer(image.read(), np.uint8), cv2.IMREAD_COLOR)
13
+ else:
14
+ # Convert uploaded image (PIL) to OpenCV format
15
+ image = np.array(image)
16
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
17
+
18
+ # Convert the image to grayscale
19
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
20
+
21
+ # Convert back to RGB for display
22
+ processed_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2RGB)
23
+ return Image.fromarray(processed_image)
24
+
25
+ # Create the Gradio interface
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown("# Image Capture and Processing App")
28
+ gr.Markdown("Upload an image or use your webcam to capture one, and convert it to grayscale!")
29
+
30
+ # Input section
31
+ with gr.Row():
32
+ image_input = gr.Image(source="upload", type="pil", label="Upload Image")
33
+ webcam_input = gr.Image(source="webcam", type="file", label="Capture from Webcam")
34
+
35
+ # Output section
36
+ output_image = gr.Image(type="pil", label="Processed Image")
37
+
38
+ # Button to process the uploaded image
39
+ process_button_upload = gr.Button("Process Uploaded Image")
40
+ process_button_webcam = gr.Button("Process Webcam Image")
41
+
42
+ # Link buttons to the function
43
+ process_button_upload.click(process_image, inputs=[image_input, gr.Checkbox(value=False)], outputs=output_image)
44
+ process_button_webcam.click(process_image, inputs=[webcam_input, gr.Checkbox(value=True)], outputs=output_image)
45
+
46
+ demo.launch()