Best Python code snippet using autotest_python
cut_segmentation_mod.py
Source:cut_segmentation_mod.py
1import numpy as np2import matplotlib.pyplot as plt3import cv24import os5import shutil6from logging import getLogger, StreamHandler, DEBUG7from file_io import write_csv89# é¾å¤ã®è¨å®10CUT_THRESHOLD = 83 # ã«ããåå²æã®é¾å¤11CUT_BETWEEN_THRESHOLD = 90 # ã«ããéãã¬ã¼ã ã®åé¤æã®é¾å¤12HIST_THRESHOLD = 0.8 # ãã¹ãã°ã©ã ã¤ã³ã¿ã»ã¯ã·ã§ã³ï¼HIï¼ã®é¡ä¼¼åº¦æ¯è¼æã®é¾å¤13FLASH_THRESHOLD = 0.65 # ãã©ãã·ã¥æ¤åºæã®HIã®é¡ä¼¼åº¦æ¯è¼æã®é¾å¤14EFFECT_THRESHOLD = 0.8 # ã¨ãã§ã¯ãæ¤åºæã®HIã®é¡ä¼¼åº¦æ¯è¼æã®é¾å¤1516# ãã°è¨å®17logger = getLogger(__name__)18handler = StreamHandler()19handler.setLevel(DEBUG)20logger.setLevel(DEBUG)21logger.addHandler(handler)22logger.propagate = False2324def dest_folder_create(dest_path):25 """26 ä¿åå
ãã©ã«ããä½æããé¢æ°2728 Parameters29 ----------30 dest_path : str31 ä¿åå
ãã©ã«ãã®ãã¹32 """33 # ä¿åå
ãã©ã«ãã®ä½æ34 # æ¢ã«åå¨ããå ´åã¯åé¤35 if os.path.exists(dest_path):36 shutil.rmtree(dest_path) # ãã©ã«ãåé¤37 os.mkdir(dest_path) # ä¿åå
ãã©ã«ãã®ä½æ3839def MSE(diff): 40 """41 å¹³åäºä¹èª¤å·®(MSE)ãè¡ã£ã¦ãçµæã帰ãé¢æ°4243 diff = y[i+1] - y[i] ï¼å·®åç»åï¼44 MSE = 1/n Σ (diff)^2 ï¼å¹³åäºä¹èª¤å·®ï¼4546 Parameters47 ----------48 diff : numpy.ndarray49 å¹³åäºä¹èª¤å·®ãæ±ããããã¼ã¿5051 Returns52 -------53 numpy.float6454 ã«ããæ¤åºç¹ï¼ãã¬ã¼ã çªå·ï¼ã®ãªã¹ã 55 """56 return np.mean(np.square(diff))5758def target_delete(cut_point, delete_target):59 """60 ã«ããç¹ã®ä¿®æ£æã«ãä¸è¦ã«ãªã£ããã¬ã¼ã ãåé¤ããé¢æ°6162 Parameters63 ----------64 cut_point : list65 ã«ããç¹ã®ãã¬ã¼ã çªå·ãªã¹ã6667 delete_target : list68 ä¸è¦ãªãã¬ã¼ã çªå·ãªã¹ã6970 Returns71 -------72 cut_point : list73 ã«ããç¹ã®ãã¬ã¼ã çªå·ãªã¹ãï¼åé¤å¾ï¼74 """75 delete_target = list(set(delete_target)) # éè¤ãæé¤7677 # åé¤å¯¾è±¡ã®ã«ããç¹ãåé¤78 for i in delete_target:79 cut_point.remove(i) # åé¤80 81 return cut_point8283def cut_between_frame_delete(cut_point, diff_images):84 """85 ã«ããéãã¬ã¼ã ãåé¤ããé¢æ°86 ã«ããéãã¬ã¼ã = ã«ããã¨ã«ããã«ç¨åºæ¥ãä¸è¦ãªãã¬ã¼ã 8788 [åé¤ã®çç±]89 ã«ããç¹ã¨èª¤æ¤åºãã¦ããç¶æ
ã®ããåé¤ããå¿
è¦ããã90 [æ¹æ³]91 å¿
ãæ£è§£ã®ã«ããç¹ã®1ã¤å¾ãã®ãã¬ã¼ã ã«åºæ¥ããããé£ç¶ãªã«ããç¹ãæ¢ãï¼åé¤åè£ï¼92 ã«ããéãã¬ã¼ã ã®åå¾ã®ãã¬ã¼ã ã®å·®åç»åãMSEã«ããã93 ãã®çµæãé¾å¤ä»¥ä¸ï¼ã»ã¨ãã©åãï¼ãªãã«ããéãã¬ã¼ã ã§ãããåé¤å¯¾è±¡94 ã«ããéãã¬ã¼ã ãåé¤9596 Parameters97 ----------98 cut_point : list99 ã«ããç¹ã®ãã¬ã¼ã çªå·ãªã¹ã100101 diff_images : numpy.ndarray102 é£æ¥ãã¬ã¼ã éã®å·®åç»å103104 Returns105 -------106 cut_point : list107 ã«ããç¹ï¼ã®ãã¬ã¼ã çªå·ãªã¹ãï¼108 """109 diff = np.diff(np.array(cut_point)).tolist() # åå¾ãã¬ã¼ã ã®ãã¬ã¼ã çªå·ã§å·®ç°ãåãï¼æä½ã®ããã«ä¸åº¦åå¤æï¼110 diff_index = [i for i, diff in enumerate(diff) if diff == 1] # å·®ç°ã1ãã¬ã¼ã ï¼é£ç¶ãªã«ããç¹ï¼ã®æ·»ãåãåå¾111 remove_candidate = [cut_point[i+1] for i in diff_index] # åé¤åè£112 113 delete_target = [] # åé¤å¯¾è±¡114 for i in remove_candidate:115 # å·®åç»åå士ã®MSEãé¾å¤ä»¥ä¸ã®ã¨ãåé¤å¯¾è±¡116 if MSE(diff_images[i-1] - diff_images[i]) <= CUT_BETWEEN_THRESHOLD:117 delete_target.append(i)118119 cut_point = target_delete(cut_point, delete_target) # åé¤å¯¾è±¡ã®å
¨ãã¬ã¼ã ãåé¤120 121 return cut_point122123def create_mask_img(img):124 """125 ãã¹ã¯ç»åãä½æããé¢æ°126 è¼åº¦ãã¹ãã°ã©ã ãæ±ããéã«ä½¿ç¨127128 Parameters129 ----------130 img : numpy.ndarray131 ãã¹ã¯ç»åãéãããç»å132133 Returns134 -------135 mask : numpy.ndarray136 ã«ããæ¤åºç¹ï¼ãã¬ã¼ã çªå·ï¼ã®ãªã¹ã 137 """138 width, height = img.shape[:2] # å¹
ãé«ã139 140 w, h = img.shape[:2]141 mask = np.zeros((width, height), np.uint8) # åæå142 mask[w//5:w//5*4, h//5:h//5*4] = 255 # ä¸å¤®ã ãéããã¹ã¯ç»å143144 return mask145146def color_histogram_comp(img1, img2, THRESHOLD, need_mask=False):147 """148 2ã¤ã®ç»åããè¼åº¦ãã¹ãã°ã©ã ã®é¡ä¼¼åº¦ãç®åºããé¢æ°149 é¡ä¼¼åº¦ã3ã¤ä»¥ä¸é¾å¤ãè¶
ããã¨ãåé¤å¯¾è±¡ã¨ãã¦æ¯è¼çµæãè¿ã150151 Parameters152 ----------153 img1 : numpy.ndarray154 æ¯è¼ç»åï¼ã®ç»åãã¼ã¿155156 img2 : numpy.ndarray157 æ¯è¼ç»åï¼ã®ç»åãã¼ã¿158 159 THRESHOLD : float160 使ç¨ããé¾å¤161162 need_mask : bool, default False163 ãã¹ã¯ç»åãé©ç¨ãããã©ãã164165 Returns166 -------167 bool168 æ¯è¼çµæï¼åé¤å¯¾è±¡ãã©ããï¼169 """170 # ç»åãã¼ã¿ããRGBã®3è²ãåãåºã171 b1, g1, r1 = img1[:,:,0], img1[:,:,1], img1[:,:,2]172 b2, g2, r2 = img2[:,:,0], img2[:,:,1], img2[:,:,2]173174 # ãã¹ã¯ç»åãé©ç¨ããå ´å175 if need_mask:176 mask = create_mask_img(img1) # ä¸å¤®ã ããéããã¹ã¯ç»åãä½æ177 # æ¯è¼ç»åï¼ã®Red, Green, BlueãGray ã®4ã¤ã®ãã¹ãã°ã©ã ä½æãã178 hist1_r = cv2.calcHist([r1],[0],mask,[256],[0,256])179 hist1_g = cv2.calcHist([g1],[0],mask,[256],[0,256])180 hist1_b = cv2.calcHist([b1],[0],mask,[256],[0,256])181 hist1_k = cv2.calcHist([img1],[0],mask,[256],[0,256])182 # æ¯è¼ç»åï¼ãåæ§ã«ä½æ183 hist2_r = cv2.calcHist([r2],[0],mask,[256],[0,256])184 hist2_g = cv2.calcHist([g2],[0],mask,[256],[0,256])185 hist2_b = cv2.calcHist([b2],[0],mask,[256],[0,256])186 hist2_k = cv2.calcHist([img2],[0],mask,[256],[0,256])187 else:188 hist1_r = cv2.calcHist([r1],[0],None,[256],[0,256])189 hist1_g = cv2.calcHist([g1],[0],None,[256],[0,256])190 hist1_b = cv2.calcHist([b1],[0],None,[256],[0,256])191 hist1_k = cv2.calcHist([img1],[0],None,[256],[0,256])192193 hist2_r = cv2.calcHist([r2],[0],None,[256],[0,256])194 hist2_g = cv2.calcHist([g2],[0],None,[256],[0,256])195 hist2_b = cv2.calcHist([b2],[0],None,[256],[0,256])196 hist2_k = cv2.calcHist([img2],[0],None,[256],[0,256])197198 # ãã¹ãã°ã©ã ã¤ã³ã¿ã»ã¯ã·ã§ã³ ã§æ¯è¼ãã¦é¡ä¼¼åº¦ãåºã199 similarity_hist_r = cv2.compareHist(hist1_r, hist2_r, cv2.HISTCMP_INTERSECT) / np.sum(hist1_r)200 similarity_hist_g = cv2.compareHist(hist1_g, hist2_g, cv2.HISTCMP_INTERSECT) / np.sum(hist1_g) 201 similarity_hist_b = cv2.compareHist(hist1_b, hist2_b, cv2.HISTCMP_INTERSECT) / np.sum(hist1_b)202 similarity_hist_k = cv2.compareHist(hist1_k, hist2_k, cv2.HISTCMP_INTERSECT) / np.sum(hist1_k) 203204 exceed_cnt = 0 # é¾å¤ãè¶
ããåæ°205206 if similarity_hist_r >= THRESHOLD:207 exceed_cnt += 1208 if similarity_hist_g >= THRESHOLD:209 exceed_cnt += 1210 if similarity_hist_b >= THRESHOLD:211 exceed_cnt += 1212 if similarity_hist_k >= THRESHOLD:213 exceed_cnt += 1214215 # é¾å¤ãè¶
ããåæ°ã3以ä¸ã®æãåé¤å¯¾è±¡ã¨ãã216 if exceed_cnt >= 3:217 return True218 else:219 return False220 221def incorrect_cut_point_delete_by_color_histogram(cut_point, frames):222 """223 è¼åº¦ãã¹ãã°ã©ã ï¼ã«ã©ã¼ãã¹ãã°ã©ã ï¼ã«ãã誤ã£ãã«ããç¹ã®åé¤ãè¡ãé¢æ°224225 [åé¤ã®çç±]226 åãã®æ¿ããæã«å¤§éãã¤é£ç¶ã§ã«ããç¹ãåã£ã¦ãã¾ãåé¡ãçãã¦ãããã227 [æ¹æ³]228 ã«ããç¹ã®ç»åãã¼ã¿ãã Red, Green, BlueãGray ã®4ã¤ã®ãã¹ãã°ã©ã ãä½æãã229 ãã¹ãã°ã©ã ã®ä½ææã«ã¯ãä¸å¤®ã®ã¿ãéããã¹ã¯ç»åã使ç¨ããï¼ä¸å¤®é¨åã®ã¿ã®è¼åº¦ãã¹ãã°ã©ã ï¼230 1ã¤å¾ãã®ã«ããç¹ã«å¯¾ãã¦ãåæ§ã«4ã¤ã®ãã¹ãã°ã©ã ãä½æãã¦2ã¤ã®ãã¹ãã°ã©ã ãæ¯è¼ãã231 æ¯è¼ã«ã¯ããã¹ãã°ã©ã ã¤ã³ã¿ã»ã¯ã·ã§ã³ ã使ã£ã¦ãé¡ä¼¼åº¦ãåºã232 é¡ä¼¼åº¦ï¼é«ãã»ã©é¡ä¼¼ãã¦ããï¼ãé¾å¤ä»¥ä¸ã®æï¼â»ï¼ã«ããç¹ããåé¤233 â»RGBã¨Gray ã®4ã¤ã®ãã¹ãã°ã©ã ã®é¡ä¼¼åº¦ã3ã¤ä»¥ä¸é¾å¤ãè¶
ããæ234235 ã«ããç¹å士ã®é¡ä¼¼åº¦ãé«ã â 誤ã£ãã«ããç¹ã®å¯è½æ§ãé«ãï¼ã«ããç¹ã«åããããªç»åã¯åºæ¬çã«ãªãããï¼236237 ãã¹ãã°ã©ã ã¤ã³ã¿ã»ã¯ã·ã§ã³ D = Σ min(h[i] - h[i+1])ããh ã¯ãã¹ãã°ã©ã 238 239 å
·ä½çãªå¦çã®ã³ã¼ãã¯ãµãé¢æ°ã§ãã[color_histogram_comp]ã«è¨è¼240241 Parameters242 ----------243 cut_point : list244 ã«ããç¹ï¼ã®ãã¬ã¼ã çªå·ãªã¹ãï¼245246 frames : numpy.ndarray247 ãã¬ã¼ã ãã¼ã¿ï¼åç»ã®å
¨ç»åãã¼ã¿ï¼248249 Returns250 -------251 cut_point : list252 ã«ããéãã¬ã¼ã ãåé¤ããå¾ã®ã«ããç¹253 """254 cut_frame = [frames[i] for i in cut_point] # ã«ããç¹ã®ç»åãã¼ã¿ï¼ãã¬ã¼ã ï¼255 delete_target = [] # åé¤å¯¾è±¡256257 for i in range(len(cut_frame)-1):258 isdelete = color_histogram_comp(cut_frame[i], cut_frame[i+1], HIST_THRESHOLD, need_mask=True) # 2ã¤ã®ç»åã®æ¯è¼çµæï¼åé¤å¯¾è±¡ãã©ããï¼259260 # åé¤å¯¾è±¡ã®æ261 if isdelete:262 delete_target.append(cut_point[i])263264 cut_point = target_delete(cut_point, delete_target) # åé¤å¯¾è±¡ã®å
¨ãã¬ã¼ã ãåé¤265 266 return cut_point267268def flash_frame_delete(cut_point, frames):269 """270 ãã©ãã·ã¥ãæ¤åºãã¦ã該å½ã«ããç¹ãåé¤ããé¢æ°271 [æ¹æ³]272 次ã®ã«ããç¹ã¨ã®ãã¬ã¼ã å·®ã5ãã¬ã¼ã 以å
ã®ã«ããç¹ã®ã¿æ¤æ»ãã273 次ã®ã«ããç¹ãå«ãã¦å¹
3ãã¬ã¼ã åã®ç»åãåå¾ããã274 åå¾ããè¤æ°ã®ç»åã®åç»ç´ ã®æå°å¤ãåã£ãç»åãä½æãã275 該å½ã«ããç¹ã®ç»åã¨ä½æããæå°ç»ç´ ã®ç»åã§è¼åº¦ãã¹ãã°ã©ã ã®é¡ä¼¼åº¦ãç®åº276 é¡ä¼¼åº¦ãé¾å¤ä»¥ä¸ã®æããã©ãã·ã¥ã®å¯è½æ§ãããã®ã§ã«ããç¹ããåé¤277 278 ãã©ãã·ã¥ã¯ä¸å¤®ä»¥å¤ã®å ´æã§ãèµ·ããããããã¹ãã°ã©ã ä½ææã®ãã¹ã¯ç»åã¯é©ç¨ããªã279280 Parameters281 ----------282 cut_point : list283 ã«ããç¹ï¼ã®ãã¬ã¼ã çªå·ãªã¹ãï¼284285 frames : numpy.ndarray286 ãã¬ã¼ã ãã¼ã¿ï¼åç»ã®å
¨ç»åãã¼ã¿ï¼287288 Returns289 -------290 cut_point : list291 ã«ããéãã¬ã¼ã ãåé¤ããå¾ã®ã«ããç¹292 """293 delete_target = [] # åé¤å¯¾è±¡294 for i in range(len(cut_point)-1):295 # 次ã®ã«ããç¹ã¨ã®ãã¬ã¼ã å·®ã5ãã¬ã¼ã 以å
ã®æ296 if abs(cut_point[i] - cut_point[i+1]) <= 5: 297 # 3ãã¬ã¼ã åã®ç»åãåå¾298 range_images = [] 299 for at in range(3):300 # æå¾ã®ãã¬ã¼ã çªå·ãè¶
ããå ´åã¯ãæå¾ã®ãã¬ã¼ã ã«ãã301 if cut_point[i+1] + at >= cut_point[-1]:302 range_images.append(frames[cut_point[-1]]) 303 else: 304 range_images.append(frames[cut_point[i+1] + at]) 305 # æ¯è¼ç»åã®ä½æ306 prev_frame = frames[cut_point[i]] # æ¯è¼å¯¾è±¡1307 next_frame = np.min(range_images, axis=0) # æ¯è¼å¯¾è±¡2308309 isdelete = color_histogram_comp(prev_frame, next_frame, FLASH_THRESHOLD, need_mask=False) # 2ã¤ã®ç»åã®æ¯è¼çµæï¼åé¤å¯¾è±¡ãã©ããï¼310311 # åé¤å¯¾è±¡ã®æ312 if isdelete:313 delete_target.append(cut_point[i])314 delete_target.append(cut_point[i+1])315316 cut_point = target_delete(cut_point, delete_target) # åé¤å¯¾è±¡ã®å
¨ãã¬ã¼ã ãåé¤317 318 return cut_point319320def effect_frame_delete(cut_point, frames):321 """322 ã¨ãã§ã¯ããæ¤åºãã¦ã該å½ã«ããç¹ãåé¤ããé¢æ°323 [æ¹æ³]324 次ã®ã«ããç¹ã¨ã®ãã¬ã¼ã å·®ã5ãã¬ã¼ã 以å
ã®ã«ããç¹ã®ã¿æ¤æ»ãã325 次ã®ã«ããç¹ããå¹
5ãã¬ã¼ã åã®ç»åãåå¾ãã326 該å½ã«ããç¹ã®ç»åã¨åå¾ãã5ã¤ã®ç»åã§è¼åº¦ãã¹ãã°ã©ã ã®é¡ä¼¼åº¦ãç®åºï¼5ååï¼327 é¡ä¼¼åº¦ãé¾å¤ä»¥ä¸ã®æãã¨ãã§ã¯ãã®å¯è½æ§ãããã®ã§ã«ããç¹ããåé¤328 329 Parameters330 ----------331 cut_point : list332 ã«ããç¹ï¼ã®ãã¬ã¼ã çªå·ãªã¹ãï¼333334 frames : numpy.ndarray335 ãã¬ã¼ã ãã¼ã¿ï¼åç»ã®å
¨ç»åãã¼ã¿ï¼336337 Returns338 -------339 cut_point : list340 ã«ããéãã¬ã¼ã ãåé¤ããå¾ã®ã«ããç¹341 """342 delete_target = []343 for i in range(len(cut_point)-1):344 if abs(cut_point[i] - cut_point[i+1]) <= 5: 345 for at in range(1, 6): # 5ãã¬ã¼ã å346 prev_frame = frames[cut_point[i]] # æ¯è¼å¯¾è±¡1347348 # æå¾ã®ãã¬ã¼ã çªå·ãè¶
ããå ´åã¯ãæå¾ã®ãã¬ã¼ã ã«ãã349 if cut_point[i+1] + at >= cut_point[-1]:350 next_frame = frames[cut_point[-1]] # æ¯è¼å¯¾è±¡2351 else: 352 next_frame = frames[cut_point[i+1] + at] # æ¯è¼å¯¾è±¡2353354 isdelete = color_histogram_comp(prev_frame, next_frame, EFFECT_THRESHOLD, need_mask=False) # 2ã¤ã®ç»åã®æ¯è¼çµæï¼åé¤å¯¾è±¡ãã©ããï¼355356 # åé¤å¯¾è±¡ã®æ357 if isdelete:358 delete_target.append(cut_point[i])359 delete_target.append(cut_point[i+1])360361 delete_target = list(set(delete_target)) # éè¤ãåé¤362 cut_point = target_delete(cut_point, delete_target) # åé¤å¯¾è±¡ã®å
¨ãã¬ã¼ã ãåé¤363 364 return cut_point365366def read_video_data(input_video_path):367 """368 åç»ãèªã¿è¾¼ã¿ããã¬ã¼ã ãã¼ã¿ã¨åç»æ
å ±ãæ½åºããé¢æ°369370 Parameters371 ----------372 input_video_path : str373 åç»ã®å
¥åãã¹ 374375 Returns376 -------377 frames : numpy.ndarray378 ãã¬ã¼ã ãã¼ã¿ï¼åç»ã®å
¨ç»åãã¼ã¿ï¼379 380 video_info : list 381 åç»ãã¼ã¿ [fps, width, height]382 """383 # --------------------------------------------------384 # åç»ã®èªã¿è¾¼ã¿385 # --------------------------------------------------386 cap = cv2.VideoCapture(input_video_path)387 # ãããªãã£ããã£ã¼ãéãã¦ããªãå ´åãä¾å¤ãè¿ã388 if cap.isOpened() is False:389 raise ValueError('èªã¿è¾¼ã¿ã¨ã©ã¼ : åç»ID ' + input_video_path + 'ãä¸æãèªã¿åãã¾ããã')390 391 fps = cap.get(cv2.CAP_PROP_FPS) # FPS392 width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # å¹
393 height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # é«ã394 frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT) # ç·ãã¬ã¼ã æ°395 sec = frame_count / fps # ç§æ°396 397 # --------------------------------------------------398 # ãã¬ã¼ã æ¯ã®ç»åæ
å ±ããªã¹ãã«æ ¼ç´399 # --------------------------------------------------400 n_frames = int(frame_count) # ç·ãã¬ã¼ã æ° 401 frames = []402 while True:403 ret, frame = cap.read()404 # æå¾ã¾ã§åå¾åºæ¥ãªãã£ãå ´åãããã¾ã§ãç·ãã¬ã¼ã ã¨ãã¦æ´æ°405 if not ret:406 #n_frames = int(cap.get(cv2.CAP_PROP_POS_FRAMES))407 break408409 frames.append(frame)410 if len(frames) == n_frames:411 break412413 if cap.isOpened():414 cap.release()415416 frames = [cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) for frame in frames] # å¦çã®ãããBGRããRGBã«ãã417 video_info = [fps, width, height] # æ»ãå¤ç¨ã®åç»æ
å ±ãã¾ã¨ãã418419 return frames, video_info420421def cut_point_detect(frames):422 """423 ã«ããç¹ãæ¤åºãã¦ãè¿ãé¢æ°424 [æé ]425 1. é£æ¥ãã¬ã¼ã éã§å·®åç»åãä½æ426 2. å¤åå²åãç®åº427 3. å¤åå²åãé¾å¤ä»¥ä¸ã®æãã«ããç¹ã¨ãã¦æ½åº428 4. 段éçãªã«ããç¹ã®ä¿®æ£429 4-1 ã«ããéãã¬ã¼ã ï¼ä¸è¦ãã¬ã¼ã ï¼ãåé¤430 4-2 è¼åº¦ãã¹ãã°ã©ã ã§èª¤ã£ãã«ããç¹ãåé¤431 4-3 ãã©ãã·ã¥æ¤åºã«ãã誤ã£ãã«ããç¹ãåé¤432 4-4 ã¨ãã§ã¯ãæ¤åºã«ãã誤ã£ãã«ããç¹ãåé¤433434 Parameters435 ----------436 frames : numpy.ndarray437 ãã¬ã¼ã ãã¼ã¿ï¼åç»ã®å
¨ç»åãã¼ã¿ï¼ 438439 Returns440 -------441 cut_point : list442 ã«ããæ¤åºç¹ï¼ãã¬ã¼ã çªå·ï¼ã®ãªã¹ã 443444 """445 # --------------------------------------------------446 # 1. é£æ¥ãã¬ã¼ã éã§å·®åç»åãä½æ447 # --------------------------------------------------448 diff_images = [aft - bef for bef, aft in zip(frames, frames[1:])] # å·®åç»å449450 # --------------------------------------------------451 # 2. å¤åå²åãç®åº452 # --------------------------------------------------453 diff_rates = [MSE(d) for d in diff_images] # å·®åç»åã®MSEãå¤åå²åã¨ãã454 455 # --------------------------------------------------456 # 3. å¤åå²åãé¾å¤ä»¥ä¸ã®æãã«ããç¹ã¨ãã¦æ½åºãã 457 # --------------------------------------------------458 cut_point = [] # ã«ããç¹ã®ãã¬ã¼ã çªå·ãªã¹ã459 for i in range(len(diff_images)):460 # å¤åå²åãé¾å¤ã以ä¸ã®æ461 if diff_rates[i] >= CUT_THRESHOLD: 462 cut_point.append(i) # ãªã¹ãã«è¿½å 463464 # --------------------------------------------------465 # 4. 段éçãªã«ããç¹ã®ä¿®æ£466 #--------------------------------------------------467 # 4-1 ã«ããéãã¬ã¼ã ï¼ä¸è¦ãã¬ã¼ã ï¼ãåé¤468 cut_point = cut_between_frame_delete(cut_point, diff_images)469470 # 4-2 è¼åº¦ãã¹ãã°ã©ã ã®é¡ä¼¼åº¦ã«ãã誤ã£ãã«ããç¹ãåé¤471 cut_point = incorrect_cut_point_delete_by_color_histogram(cut_point, frames) 472473 # 4-3 ãã©ãã·ã¥æ¤åºã«ãã誤ã£ãã«ããç¹ãåé¤474 cut_point = flash_frame_delete(cut_point, frames)475476 # 4-4 ã¨ãã§ã¯ãæ¤åºã«ãã誤ã£ãã«ããç¹ãåé¤477 cut_point = effect_frame_delete(cut_point, frames)478 479 cut_point.append(len(frames) - 1) # åç»ã®æå¾ã®ãã¬ã¼ã ã¤ã³ããã¯ã¹ã追å 480481 return cut_point482 483def graph_save(data, dest_path):484 """485 å¤åå²åã®ã°ã©ããä¿åããé¢æ°486487 Parameters488 ----------489 data : numpy.ndarray490 å¤åå²å491492 dest_path : str493 ä¿åå
ãã©ã«ãã®ãã¹494 """495 fig = plt.figure(figsize=(10, 5))496 plt.plot(data) 497 plt.title('Graph of Cut Change Detection') # ã¿ã¤ãã«498 plt.xlabel('frame') # X軸ã©ãã«499 plt.ylabel('Diff Rate') # Y軸ã©ãã«500 plt.grid(True) # ã°ãªãããã501 plt.axhline(85, color='red') # ã«ããããã©ã¤ã³ãæç»502 #plt.show() # ã°ã©ãæç»503 504 plt.savefig(dest_path + '\\change_rate_graph.jpg') # ä¿å505506def cut_save(video_id, cut_point, frames, video_info, dest_path):507 """508 åç»ãåå²ãã¦ä¿åããé¢æ°509510 Parameters511 ----------512 video_id : str513 åç»ID 514515 cut_point : list516 ã«ããæ¤åºç¹ï¼ãã¬ã¼ã çªå·ï¼ã®ãªã¹ã517 518 frames : numpy.ndarray519 ãã¬ã¼ã ãã¼ã¿ï¼åç»ã®å
¨ç»åãã¼ã¿ï¼ 520 521 video_info : list 522 åç»ãã¼ã¿ [fps, width, height]523 524 dest_path : str525 ä¿åå
ãã©ã«ãã®ãã¹526 """527 # --------------------------------------------------528 # ã«ããåå²ã»ä¿å529 # --------------------------------------------------530 fps, width, height = video_info # åç»æ
å ±ã®å±é531532 #fourcc = cv2.VideoWriter_fourcc('m','p','4','v') # åç»ã®ä¿åå½¢å¼533 fourcc = 0x00000021 # åç»ã®ä¿åå½¢å¼(H264å½¢å¼ã§ã¨ã³ã³ã¼ã)ã2021/2/12 å¤æ´534 writer = [] # ã«ããæ¸ãè¾¼ã¿ç¨ã®ãªã¹ã535 begin = 0 # ã«ããæåã®ãã¬ã¼ã 536537 frames = [cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) for frame in frames] # RGBããBGRã«æ»ãï¼æ»ããªãã¨è²ãå転ããã¾ã¾ä¿åãããï¼538539 cut_count = len(cut_point) # ã«ããæ°540 for i in range(cut_count):541 writer.append(cv2.VideoWriter(dest_path + '\\cut' + str(i+1) + '.mp4', fourcc, fps, (int(width), int(height))))542 for j in range(begin, cut_point[i]+1):543 writer[i].write(frames[j])544 begin = cut_point[i]+1545546 logger.debug(video_id + '_cut1 ï½ ' + str(cut_count) + 'ãä¿åãã¾ãã')547 logger.debug('ä¿åå
: ' + dest_path)548 logger.debug('--------------------------------------------------')549550 # --------------------------------------------------551 # å¤åå²åã°ã©ããä¿å552 # --------------------------------------------------553 # diff_images = [aft - bef for bef, aft in zip(frames, frames[1:])] # å·®åç»å554 # diff_rates = [MSE(d) for d in diff_images] # å·®åç»åã®MSEãå¤åå²åã¨ãã555 # graph_save(frames, dest_path)556557def cut_segmentation(files, result_cut_path):558 """559 ã«ããåå²ãè¡ããåã«ããããã©ã«ãã«ä¿åããé¢æ°560 561 以ä¸ã®æµãã§è¡ã562 563 åç»IDãªã¹ãã®ä½æ564 ã«ããåå²565 ä¿åå
ãã©ã«ãã®ä½æ 566 åç»ã®èªã¿è¾¼ã¿ããã¬ã¼ã ãã¼ã¿ï¼åç»æ
å ±ã®æ½åº567 ã«ããç¹ã®æ¤åº568 ã«ããã®ä¿å569570 Parameters571 ----------572 video_path : str573 å
¥åããåç»ãã¼ã¿ã®ãã©ã«ããã¹574575 result_cut_path576 ã«ããåå²çµæãä¿åãããã©ã«ããã¹577 """578 # --------------------------------------------------579 # ä¿åå
ã®ä¸èº«ãåæå580 # --------------------------------------------------581 dest_folder_create(result_cut_path)582583 # --------------------------------------------------584 # åç»IDãªã¹ãã®ä½æ585 # --------------------------------------------------586 #files = os.listdir(video_path) # åç»ãã¡ã¤ã«åï¼åç»IDï¼ä¸è¦§ãåå¾587 video_id_list = files # ä¸è¦§ããæ¡å¼µåãåé¤588 589 # --------------------------------------------------590 # ã«ããåå²591 # --------------------------------------------------592 cut_point_list = [] # ã«ããç¹ã®ãªã¹ã593 for video_id in video_id_list:594 input_video_path = video_id # åç»ãã¡ã¤ã«ã®å
¥åãã¹ 595 596 # --------------------------------------------------597 # ä¿åå
ãã©ã«ãã®ä½æ598 # --------------------------------------------------599 video_name = os.path.splitext(os.path.basename(video_id))[0] # åç»å(æ¡å¼µåãªã)600 dest_path = os.path.join(result_cut_path, video_name) # ååç»ã®ã«ããåå²çµæã®ä¿åå
601 #dest_path = result_cut_path # ååç»ã®ã«ããåå²çµæã®ä¿åå
602 603 dest_folder_create(dest_path) # ãã©ã«ãä½æ 604 605 # --------------------------------------------------606 # åç»ã®èªã¿è¾¼ã¿ããã¬ã¼ã ãã¼ã¿ã¨åç»æ
å ±ãæ½åº607 # --------------------------------------------------608 frames, video_info = read_video_data(input_video_path)609 610 # --------------------------------------------------611 # ã«ããç¹ã®æ¤åº612 # --------------------------------------------------613 cut_point = cut_point_detect(frames)614 cut_point_list.append(cut_point)615 616 # --------------------------------------------------617 # ã«ããç¹ã®æ
å ±ã使ç¨ãã¦ãåç»ãåå²ãã¦ä¿å618 # --------------------------------------------------619 cut_save(video_id, cut_point, frames, video_info, dest_path)620 621 # --------------------------------------------------622 # ã«ããç¹ã®ãªã¹ããCSVãã¡ã¤ã«ã«ä¿åï¼å¾ã®å¦çã§å使ç¨ããããï¼623 # --------------------------------------------------624 # TODO ãã¼ãã³ãã¼ããã625 write_csv([video_id_list, cut_point_list], r'C:\Users\fukuyori\Scene-Analyzer\resources\app\python\temp\cut_point.csv')626627
...
脚本集合.py
Source:脚本集合.py
...64# å é¤æ´»å¨æ°æ® 65def delete_activity():66 _activity = mongo.re_pos_int(input("å ä»ä¹æ´»å¨ï¼1.ææé¶è¡ï¼2.å®ç®±æ¿å±ï¼3.å¨å¨æºï¼4.éæ¶æ´»å¨ï¼5.ç©å
·ä¹å®¶ï¼6ç»éä»»å¡ï¼7.è¿æ°ä¼> "))67 if _activity == 1:68 delete_target("inspire_bank_user")69 elif _activity == 2:70 delete_target("inspire_user_job")71 elif _activity == 3:72 delete_target("prize_claw_user", "prize_claw_team", "prize_claw_not_full_team")73 elif _activity == 4:74 delete_target("time_limit_user", "time_limit_rank_score", "time_limit_rank_backup", "time_limit_rank", "time_limit_garden_war")75 elif _activity == 5:76 delete_target("toy_user_job")77 elif _activity == 6:78 delete_target("new_mission_user")79 elif _activity == 7:80 delete_target("new_task_user")81 else:82 _activity = mongo.re_pos_int(input("è¾éäºï¼éæ°è¾å
¥> "))83 Del_data(_activity)84 85 pass86def delete_target(COLLECTION, *depends):87 db_coll = db[COLLECTION]88 _id = input("å åªä¸ªID> ")89 target = {'_id': mongo.re_pos_int(_id)}90 result_find = db_coll.find_one(target)91 # print(db_coll.find()[0])92 if result_find:93 result = db_coll.delete_one(target)94 print("å é¤æå")95 time.sleep(1)96 else:97 print("æªæ¾å°æ¤ID,éæ°è¾å
¥> ")98 delete_target(COLLECTION)99 for depend in depends:100 db_coll = db[depend]101 depend_find = db_coll.find_one(target)102 # å é¤æè¡æ¦ç表åä¸æ¤UserIDçç¸å
³æ°æ®(æ¾å°äºå°±å ï¼æ¾ä¸å°è¿å¨å¨æºé»è¾)103 # ä¸å¤ä¸¥è°¨ï¼éæ¶æ´»å¨æç表å没æ¾å°ä¸è¥¿ä¹ä¼èµ°elif104 if depend_find:105 result = db_coll.delete_one(target)106 # å¨å¨æºæ´»å¨ï¼teamID=æ¥æ+UserIDï¼å é¤teamä¸ä¸ªäººä¿¡æ¯107 # 'b'å段æ¯æ¯ä¸ªidçéä¼id108 elif result_find['b'] != 0:109 # éä¼ç¸å
³ä¿¡æ¯110 target_team = {'_id': result_find['b']}111 find_team = db_coll.find_one(target_team)112 # è½æ¾å°éä¼å°±æ¯å¨å¨æºæ´»å¨ï¼æ¾ä¸å°å°±æ¯å
¶å®ä¸ç¨å¨çæ´»å¨æ°æ®(æ¯å¦éæ¶æ´»å¨å
¶å®depends)...
删除活动数据.py
Source:删除活动数据.py
2import time3from mongo import mongodb4from mongo import re_pos_int5db = mongodb()6def delete_target(COLLECTION, *depends):7 db_coll = db[COLLECTION]8 _id = input("å åªä¸ªID> ")9 target = {'_id': re_pos_int(_id)}10 result_find = db_coll.find_one(target)11 # print(db_coll.find()[0])12 if result_find:13 result = db_coll.delete_one(target)14 print("å é¤æå")15 time.sleep(1)16 else:17 print("æªæ¾å°æ¤ID,éæ°è¾å
¥> ")18 delete_target(COLLECTION)19 for depend in depends:20 21 db_coll = db[depend]22 depend_find = db_coll.find_one(target)23 # å é¤æè¡æ¦ç表åä¸æ¤UserIDçç¸å
³æ°æ®(æ¾å°äºå°±å ï¼æ¾ä¸å°è¿å¨å¨æºé»è¾)24 # ä¸å¤ä¸¥è°¨ï¼éæ¶æ´»å¨æç表å没æ¾å°ä¸è¥¿ä¹ä¼èµ°elif25 if depend_find:26 result = db_coll.delete_one(target)27 # å¨å¨æºæ´»å¨ï¼teamID=æ¥æ+UserIDï¼å é¤teamä¸ä¸ªäººä¿¡æ¯28 # 'b'å段æ¯æ¯ä¸ªidçéä¼id29 elif result_find['b'] != 0:30 # éä¼ç¸å
³ä¿¡æ¯31 target_team = {'_id': result_find['b']}32 find_team = db_coll.find_one(target_team)33 # è½æ¾å°éä¼å°±æ¯å¨å¨æºæ´»å¨ï¼æ¾ä¸å°å°±æ¯å
¶å®ä¸ç¨å¨çæ´»å¨æ°æ®(æ¯å¦éæ¶æ´»å¨å
¶å®depends)34 if find_team:35 # ä¸ä¸ªäººä¸éå°±å éï¼å¦ååªå èªå·±36 if isinstance(find_team['a'], dict):37 if len(find_team['a']) != 1:38 del find_team['a'][_id]39 result = db_coll.update_one(target_team, {'$set': find_team})40 else:41 result = db_coll.delete_one(find_team) 42 # å人ååæ¶åå ænot_fulléä¼43 elif find_team['a'] == 1:44 result = db_coll.delete_one(find_team)45def Del_data(_activity): 46 if _activity == 1:47 delete_target("inspire_bank_user")48 elif _activity == 2:49 delete_target("inspire_user_job")50 elif _activity == 3:51 delete_target("prize_claw_user", "prize_claw_team", "prize_claw_not_full_team")52 elif _activity == 4:53 delete_target("time_limit_user", "time_limit_rank_score", "time_limit_rank_backup", "time_limit_rank", "time_limit_garden_war")54 elif _activity == 5:55 delete_target("toy_user_job")56 elif _activity == 6:57 delete_target("new_mission_user")58 elif _activity == 7:59 delete_target("new_task_user")60 elif _activity == 8:61 delete_target("camp_war_user")62 else:63 _activity = int(input("è¾éäºï¼éæ°è¾å
¥> "))64 Del_data(_activity)65_activity = int(input("å ä»ä¹æ´»å¨ï¼1.ææé¶è¡ï¼2.å®ç®±æ¿å±ï¼3.å¨å¨æºï¼4.éæ¶æ´»å¨ï¼5.ç©å
·ä¹å®¶ï¼6ç»éä»»å¡ï¼7.è¿æ°ä¼, 8.éµè¥æ> "))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!