Spaces:
Sleeping
Sleeping
Raumkommander
commited on
Commit
·
2d6fc22
1
Parent(s):
ee84b3c
inital deployment1
Browse files
app.py
CHANGED
@@ -1,27 +1,13 @@
|
|
1 |
-
import torch
|
2 |
-
import gradio as gr
|
3 |
-
from diffusers import StableDiffusionPipeline, LCMScheduler
|
4 |
-
|
5 |
-
|
6 |
import gradio as gr
|
7 |
import cv2
|
|
|
8 |
import numpy as np
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
# Load the pre-trained Real-Time LCM model
|
14 |
-
model_id = "SimianLuo/LCM_Dreamshaper_v7"
|
15 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
16 |
-
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
17 |
-
pipe.to("cuda")
|
18 |
-
|
19 |
-
# Function to generate images
|
20 |
-
def generate_image(prompt: str):
|
21 |
-
image = pipe(prompt, num_inference_steps=4).images[0]
|
22 |
-
return image
|
23 |
-
|
24 |
-
|
25 |
|
26 |
def apply_color_filter(frame, filter_type="None"):
|
27 |
"""Apply a color filter to the frame."""
|
@@ -36,25 +22,39 @@ def apply_color_filter(frame, filter_type="None"):
|
|
36 |
frame[:, :, 1] = 0 # Remove green channel
|
37 |
return frame
|
38 |
|
39 |
-
def process_frame(frame, filter_type="None"):
|
40 |
-
"""Process a single frame by applying a color filter."""
|
41 |
frame = apply_color_filter(frame, filter_type)
|
42 |
-
return frame
|
43 |
-
|
44 |
-
def video_stream(frame):
|
45 |
-
"""Receives video from webcam and processes it"""
|
46 |
-
if frame is None:
|
47 |
-
return None
|
48 |
|
49 |
-
|
50 |
-
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
iface = gr.Interface(
|
56 |
fn=video_stream,
|
57 |
-
inputs=
|
58 |
-
|
|
|
|
|
|
|
59 |
live=True
|
60 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
+
import torch
|
4 |
import numpy as np
|
5 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
6 |
+
from PIL import Image
|
7 |
|
8 |
+
# Load the Stable Diffusion Model
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def apply_color_filter(frame, filter_type="None"):
|
13 |
"""Apply a color filter to the frame."""
|
|
|
22 |
frame[:, :, 1] = 0 # Remove green channel
|
23 |
return frame
|
24 |
|
25 |
+
def process_frame(frame, filter_type="None", prompt="A futuristic landscape"):
|
26 |
+
"""Process a single frame by applying a color filter and diffusion model."""
|
27 |
frame = apply_color_filter(frame, filter_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Convert frame to PIL image
|
30 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).resize((512, 512))
|
31 |
|
32 |
+
# Apply Stable Diffusion model
|
33 |
+
result = pipe(prompt=prompt, image=image, strength=0.5, guidance_scale=7.5).images[0]
|
34 |
+
return np.array(result)
|
35 |
+
|
36 |
+
def video_stream(filter_type, prompt):
|
37 |
+
"""Captures video feed from webcam, applies color filter, and sends to the AI model."""
|
38 |
+
cap = cv2.VideoCapture(0)
|
39 |
+
while cap.isOpened():
|
40 |
+
ret, frame = cap.read()
|
41 |
+
if not ret:
|
42 |
+
break
|
43 |
+
|
44 |
+
frame = process_frame(frame, filter_type, prompt)
|
45 |
+
yield frame # Return processed frame
|
46 |
+
cap.release()
|
47 |
+
|
48 |
+
# Create Gradio App
|
49 |
iface = gr.Interface(
|
50 |
fn=video_stream,
|
51 |
+
inputs=[
|
52 |
+
gr.Radio(["None", "Red", "Green", "Blue"], label="Color Filter"),
|
53 |
+
gr.Textbox(label="Stable Diffusion Prompt", value="A futuristic landscape")
|
54 |
+
],
|
55 |
+
outputs=gr.Image(label="AI-Enhanced Webcam Feed"),
|
56 |
live=True
|
57 |
)
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
iface.launch(share=True)
|