-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground_change.py
More file actions
76 lines (61 loc) · 2.26 KB
/
background_change.py
File metadata and controls
76 lines (61 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Invisible Object- Make Any Object(s) Invisible Simulataneously
# Author: Savan Visalpara
import cv2
from torchvision import models
import torch
import numpy as np
import matplotlib.pyplot as plt
import time
import sys
import os
sys.path.append("./light")
import argparse
from segment import mmodel
def background( video, outv, show, bg, dn):
cap = cv2.VideoCapture(video)
bg_img = cv2.imread(bg, cv2.IMREAD_COLOR)
bg_img = cv2.resize(bg_img, (520, 350)) # resize background image
cv2.imshow("bg", bg_img)
if outv:
# this can write .avi only
out = cv2.VideoWriter(outv,cv2.VideoWriter_fourcc('M','J','P','G'), 30, (520,350))
inv = [15] #objects to keep in foreground
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, (520, 350))
# cv2.imshow('input', frame)
output = mmodel(frame)
mask = np.zeros((350,520,3))
for i in inv:
mask[np.where(output==i)] = 255
# kernel = np.ones((30,30),np.uint8)
#dilate to include surrounding pixels which might have not be detected
# dilated = cv2.dilate(mask,kernel,iterations = 1)
# cv2.imshow("dilated", dilated)
dilated = mask
# cv2.imshow("mask", dilated)
frame[np.where(dilated==0)] = bg_img[np.where(dilated==0)]
if outv:
out.write(frame)
if show:
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if dn:
inp = input("Enter new background image or hit enter to continue")
if os.path.exists(inp):
bg_img = cv2.imread(inp, cv2.IMREAD_COLOR)
bg_img = cv2.resize(bg_img, (520, 350))
else:
print("Entered image does not exist.")
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--video", default="./assets/bgdemo3.mp4", help="path to input video")
parser.add_argument("--out_video", default="bgdemo.avi",help="path to output video") #if not passes, show on screen
parser.add_argument("--show", default=True)
parser.add_argument("--bg", default="./assets/default_bg.jpg")
parser.add_argument("--change_bg_dynamic", default=True, help="allow to change bg while running")
args = parser.parse_args()
background(args.video, args.out_video, args.show, args.bg, args.change_bg_dynamic)