|
import gradio as gr |
|
import numpy as np |
|
import cv2 |
|
|
|
def create_dot_effect(image, dot_size=10, spacing=2): |
|
|
|
if len(image.shape) == 3: |
|
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) |
|
else: |
|
gray = image |
|
|
|
|
|
height, width = gray.shape |
|
canvas = np.zeros_like(gray) |
|
|
|
|
|
y_dots = range(0, height, dot_size + spacing) |
|
x_dots = range(0, width, dot_size + spacing) |
|
|
|
|
|
for y in y_dots: |
|
for x in x_dots: |
|
|
|
region = gray[y:min(y+dot_size, height), x:min(x+dot_size, width)] |
|
if region.size > 0: |
|
brightness = np.mean(region) |
|
|
|
|
|
if brightness > 30: |
|
cv2.circle(canvas, |
|
(x + dot_size//2, y + dot_size//2), |
|
dot_size//2, |
|
(255), |
|
-1) |
|
|
|
return canvas |
|
|
|
def process_video(video_path, dot_size=10, spacing=2): |
|
|
|
cap = cv2.VideoCapture(video_path) |
|
if not cap.isOpened(): |
|
return None |
|
|
|
|
|
fps = int(cap.get(cv2.CAP_PROP_FPS)) |
|
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
|
|
|
|
output_path = "temp_output.mp4" |
|
fourcc = cv2.VideoWriter_fourcc(*'avc1') |
|
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height), False) |
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
|
|
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
|
|
|
dotted_frame = create_dot_effect(frame_rgb, dot_size, spacing) |
|
out.write(dotted_frame) |
|
|
|
cap.release() |
|
out.release() |
|
|
|
return output_path |
|
|
|
|
|
with gr.Blocks(title="ChatGPT Ad Maker") as iface: |
|
gr.Markdown("# ChatGPT Ad Maker") |
|
gr.Markdown("Convert your image or video into a dotted pattern. Adjust dot size and spacing using the sliders.") |
|
|
|
with gr.Tab("Image"): |
|
image_input = gr.Image(label="Input Image") |
|
with gr.Row(): |
|
img_dot_size = gr.Slider(minimum=2, maximum=20, value=10, step=1, label="Dot Size") |
|
img_spacing = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Dot Spacing") |
|
image_output = gr.Image(label="Dotted Output") |
|
image_button = gr.Button("Process Image") |
|
image_button.click( |
|
fn=create_dot_effect, |
|
inputs=[image_input, img_dot_size, img_spacing], |
|
outputs=image_output |
|
) |
|
|
|
with gr.Tab("Video"): |
|
video_input = gr.Video(label="Input Video") |
|
with gr.Row(): |
|
vid_dot_size = gr.Slider(minimum=2, maximum=20, value=10, step=1, label="Dot Size") |
|
vid_spacing = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Dot Spacing") |
|
video_output = gr.Video(label="Dotted Output", format="mp4") |
|
video_button = gr.Button("Process Video") |
|
video_button.click( |
|
fn=process_video, |
|
inputs=[video_input, vid_dot_size, vid_spacing], |
|
outputs=video_output |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|