---
# System prepended metadata

title: 修改Mask R-CNN demo_video
tags: [Mask R-CNN]

---

###### tags: `Mask R-CNN`

# 修改Mask R-CNN demo_video

### 濃度

visualize.py

alpha控制濃淡度
```python=79
def apply_mask(image, mask, color, alpha=0.7):
```

### 小範圍不要mask

video_demo.py

x1,x2,y1,y2 為畫框的座標點
```python=54
if ((int(((x2 - x1)**2)**0.5) * int(((y2-y1)**2)**0.5)) < 10000):
    break
```

### 修改影片執行Mask效率

video_demo.py

#### 概念：設定一個控制器 frame_rate_divider = 0，每當他=10的時候執行一次辨識，10禎一次
```python=111
#控制影片執行mask次數 
frame_rate_divider = 0
while(cap.isOpened()):
    stime = time.time()
    ret, frame = cap.read()
    result_frame = frame
    if ret:
        # 設定為10 代表10貞執行一次mask
        if frame_rate_divider == 10:
            results = model.detect_keypoint([frame], verbose=0)
            r = results[0]
         # for one image
            log("rois", r['rois'])
            log("keypoints", r['keypoints'])
            log("class_ids", r['class_ids'])
            log("keypoints", r['keypoints'])
            log("masks", r['masks'])
            log("scores", r['scores'])
            result_frame = cv2_display_keypoint(frame,r['rois'],r['keypoints'],r['masks'],r['class_ids'],r['scores'],class_names)
            
            cv2.imshow('frame', result_frame)
            frame_rate_divider = 0

            tEnd = time.time()
            tMin = (int(tEnd - tStart)) // 60
            print("use time : " + str(tMin) +" Min " + str(int(tEnd - tStart) - tMin * 60) + " sec ", end = "")
        else:
            frame_rate_divider += 1
        
        #輸出影片 放在控制mask次數外面才能每一貞都儲存下來
        output.write(result_frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
```