GIGAParviz commited on
Commit
ce890b6
·
verified ·
1 Parent(s): 608c6a9

Upload 3 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. app.py +90 -0
  3. example.mp4 +3 -0
  4. target (2).png +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ example.mp4 filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+
5
+ EXAMPLE_VIDEO = "example.mp4"
6
+ EXAMPLE_IMAGE = "target (2).png"
7
+
8
+ def process_video_and_image(video_path, target_image_path):
9
+ try:
10
+
11
+ target_img = cv2.imread(target_image_path)
12
+ target_gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY)
13
+
14
+ cap = cv2.VideoCapture(video_path)
15
+
16
+ sift = cv2.SIFT_create()
17
+ keypoints_target, descriptors_target = sift.detectAndCompute(target_gray, None)
18
+ flann = cv2.FlannBasedMatcher(dict(algorithm=1, trees=5), dict(checks=50))
19
+
20
+ output_frames = []
21
+
22
+ while cap.isOpened():
23
+ ret, frame = cap.read()
24
+ if not ret:
25
+ break
26
+
27
+ frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
28
+ keypoints_frame, descriptors_frame = sift.detectAndCompute(frame_gray, None)
29
+
30
+ if descriptors_frame is not None:
31
+ matches = flann.knnMatch(descriptors_target, descriptors_frame, k=2)
32
+ good_matches = [m for m, n in matches if m.distance < 0.70 * n.distance]
33
+
34
+ if len(good_matches) > 10:
35
+ src_pts = [keypoints_target[m.queryIdx].pt for m in good_matches]
36
+ dst_pts = [keypoints_frame[m.trainIdx].pt for m in good_matches]
37
+
38
+ src_pts = np.float32(src_pts).reshape(-1, 1, 2)
39
+ dst_pts = np.float32(dst_pts).reshape(-1, 1, 2)
40
+ matrix, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
41
+
42
+ if matrix is not None:
43
+ h, w = target_gray.shape
44
+ pts = np.float32([[0, 0], [0, h], [w, h], [w, 0]]).reshape(-1, 1, 2)
45
+ dst = cv2.perspectiveTransform(pts, matrix)
46
+ frame = cv2.polylines(frame, [np.int32(dst)], isClosed=True, color=(0, 255, 0), thickness=3)
47
+
48
+ match_frame = cv2.drawMatches(target_img, keypoints_target, frame, keypoints_frame, good_matches, None)
49
+ output_frames.append(cv2.resize(match_frame, (1200, 600)))
50
+
51
+ cap.release()
52
+
53
+ height, width, _ = output_frames[0].shape
54
+ out_video_path = "output_video.avi"
55
+ out = cv2.VideoWriter(out_video_path, cv2.VideoWriter_fourcc(*'XVID'), 10, (width, height))
56
+
57
+ for frame in output_frames:
58
+ out.write(frame)
59
+ out.release()
60
+
61
+ return out_video_path
62
+ except Exception as e:
63
+ return f"Error: {str(e)}"
64
+
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("## Video and Target Image Matcher")
67
+
68
+ with gr.Row():
69
+ video_input = gr.File(label="Upload Video File", file_types=[".mp4", ".avi", ".mov"])
70
+ image_input = gr.File(label="Upload Target Image", file_types=[".png", ".jpg", ".jpeg"])
71
+ example_button = gr.Button("Process Example")
72
+
73
+ output_video = gr.Video(label="Matched Video Output")
74
+ process_button = gr.Button("Process")
75
+
76
+ def process_example():
77
+ return process_video_and_image(EXAMPLE_VIDEO, EXAMPLE_IMAGE)
78
+
79
+ process_button.click(
80
+ fn=process_video_and_image,
81
+ inputs=[video_input, image_input],
82
+ outputs=output_video
83
+ )
84
+ example_button.click(
85
+ fn=process_example,
86
+ inputs=[],
87
+ outputs=output_video
88
+ )
89
+
90
+ demo.launch()
example.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ec88256467e2bd1d07f74b1958fc689538c4b0aae442502fa0a33bb587fba2a2
3
+ size 29809959
target (2).png ADDED