File size: 8,279 Bytes
ceb119d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import PIL
from PIL import ImageDraw, ImageFont, Image ,ImageOps ,ImageFilter
from ultralytics import YOLO
import warnings
import cv2
import numpy as np
import subprocess
import os
import matplotlib.pyplot as plt

warnings.filterwarnings("ignore", category=FutureWarning)

class SegmenterBackground():
    def __init__(self) -> None:
        self.segment_names = {}  # This dictionary will store names for segments across multiple inferences
        self.person=['person']
        self.animal=[ 'bird',  'cat',  'dog',  'horse', 'sheep',  'cow', 'elephant',  'bear','zebra', 'giraffe']
        self.drive=['bicycle','car','motorcycle', 'airplane', 'bus', 'train','truck','boat']

    

    def predict_image(self,raw_image: Image):
        model = YOLO("yolov8n-seg.pt")
        class_names = model.names
        results = model(raw_image)
        return results, class_names



    def assign_segment_name(self,label, segment_id):
        """ Assigns a unique name for each detected segment (e.g., Person 1, Person 2). """
        if label not in self.segment_names:
            self.segment_names[label] = {}
        
        if segment_id not in self.segment_names[label]:
            segment_count = len(self.segment_names[label]) + 1
            self.segment_names[label][segment_id] = f"{label} {segment_count}"
        
        return self.segment_names[label][segment_id]


    def putMaskImage(self,raw_image,masks,background_image="remove",blur_radius=23):
        combined_mask = np.max(masks, axis=0)

        # Create mask for areas to replace
        mask = combined_mask == True

        # Ensure the mask has the same shape as the raw image (broadcast the mask to RGB channels)
        mask_rgb = np.stack([mask] * 3, axis=-1)

        # Initialize the output array as a copy of the background image
        ##outpt = np.array(background_image.copy())
        if type(background_image)==PIL.Image.Image: # not PIL.JpegImagePlugin.JpegImageFile as resized
            outpt = np.array(background_image.copy())
        elif(background_image=="cam"):
            outpt=np.array(raw_image.filter(ImageFilter.GaussianBlur(radius=blur_radius)))
        else:#default ,say on "remove"
            outpt=np.zeros_like(raw_image)

        # Replace the background in the output image with the raw image where the mask is True
        outpt[mask_rgb] = np.array(raw_image)[mask_rgb]

        # Resize the output for better experience
        outpt = Image.fromarray(outpt)

        return outpt


    def getFont(self):
        try:
            font = ImageFont.truetype("arial.ttf", size=20)  
        except IOError:
            font = ImageFont.load_default()
        return font

    def Back_step1(self,raw_image: Image, background_image: Image,blur_radius=23):
        org_size = raw_image.size
        raw_image = raw_image.resize((640, 480))
        if type(background_image) == PIL.JpegImagePlugin.JpegImageFile:
            background_image = background_image.resize((640, 480))
        label_counter = []


        results, class_names = self.predict_image(raw_image)
        
        masks = [results[0].masks.data[i].cpu().numpy() for i in range(len(results[0].masks.data))]

        ##### put masks on image
        outpt = self.putMaskImage(raw_image,masks,background_image,blur_radius)
        
        # Draw bounding boxes and labels
        font=self.getFont()
        draw = ImageDraw.Draw(outpt) 

        
        
        for box, label, seg_id in zip(results[0].boxes.xyxy.cpu().numpy(), 
                                    results[0].boxes.cls.cpu().numpy(), 
                                    range(len(results[0].boxes))):  # segment_id for each box
            label_name = class_names[int(label)]
            
            # Assign a unique name for each detected object based on its segment
            current_label = self.assign_segment_name(label_name, seg_id)

            x1, y1, x2, y2 = map(int, box)
            
            draw.rectangle([x1, y1, x2, y2], outline="red", width=2)
            
            draw.text((x1, y1), current_label+" " + str(seg_id), fill="black", font=font)

            label_counter.append(current_label)

        return outpt.resize(org_size), label_counter


    def Back_step2(self,raw_image:Image,background_image:Image,things_replace:list,blur_radius=23):
        org_size = raw_image.size
        raw_image = raw_image.resize((640, 480))
        print(type(background_image))
        if type(background_image)==PIL.JpegImagePlugin.JpegImageFile:
            background_image = background_image.resize((640, 480))


        results, class_names = self.predict_image(raw_image)
        
        masks=[]
        for segm, label,seg_id in zip(results[0].masks.data,results[0].boxes.cls.cpu().numpy(),range(len(results[0].boxes))): 
            label_name = class_names[int(label)]
            current_label = self.assign_segment_name(label_name, seg_id)

            if current_label in things_replace:
                masks.append(segm.cpu().numpy())

        masked_image=self.putMaskImage(raw_image,masks,background_image,blur_radius)
        return masked_image.resize(org_size)



    def get_labels(self,kind_back):
        list_output=[]

        if ('person' in kind_back):
            list_output=list_output + self.person
        if ('animal' in kind_back):
            list_output=list_output + self.animal
        if ('drive' in kind_back):
            list_output=list_output + self.drive

        return list_output


    def Back_video_step1(self,video_path,output_path,background_image,kind_back,blur_radius=35):#background_image,what_remove,blur_radius=23): # back_image and video? #what_remove if many person it is not identify so same
        cap = cv2.VideoCapture(video_path)
        
        # Get video properties
        frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(cap.get(cv2.CAP_PROP_FPS))
        #number_frames= int(cv2.CAP_PROP_FRAME_COUNT)
        
        # Define the codec and create VideoWriter object to save the output video
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
        if isinstance(background_image, Image.Image):
            background_image = background_image.resize((640, 480))

        
        sound_tmp_file='audio.mp3'
        if os.path.exists(sound_tmp_file):# to not give error
            os.remove(sound_tmp_file)
            subprocess.run(['ffmpeg', '-i', video_path, '-q:a', '0', '-map', 'a',sound_tmp_file])
        else:
            subprocess.run(['ffmpeg', '-i', video_path, '-q:a', '0', '-map', 'a',sound_tmp_file])



        i=0
        while True:

            ret, frame = cap.read()
            if not ret:
                break  # End of video
            
            # Convert the current frame to RGB
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame_rgb = Image.fromarray(np.array(frame_rgb))
            org_size = frame_rgb.size
            frame_rgb = frame_rgb.resize((640, 480))

            results,class_names = self.predict_image(frame_rgb)

            masks=[] 
            
            things_replace=self.get_labels(kind_back)
            
            for segm, label in zip(results[0].masks.data,results[0].boxes.cls.cpu().numpy()): 
                label_name = class_names[int(label)]

                if label_name in things_replace:
                    masks.append(segm.cpu().numpy())

            masked_image = self.putMaskImage(frame_rgb,masks,background_image,blur_radius)


            out.write(cv2.cvtColor(np.array(masked_image.resize(org_size)), cv2.COLOR_RGB2BGR))

            print(f"Completed frame {i+1} ")
            i=i+1
            
            #if (i==10):
            #    break

        print("Finished frames")
        # adding original sound ,by extracting sound then put in new video

        cap.release()
        out.release()
        cv2.destroyAllWindows()
        
        
        #subprocess.run(['ffmpeg', '-i', output_path, '-i', 'audio.mp3', '-c:v', 'copy', '-c:a', 'aac', '-strict', 'experimental',"temp_output_video.mp4" ])
        os.remove(sound_tmp_file)