關於物體(車輛)震顫(熄火)檢測研究

關 於 物 體 ( 車 輛 ) 震 顫 ( 熄 火 ) 檢 測 研 究 關於物體(車輛)震顫(熄火)檢測研究 關於物體(車輛)震顫(熄火)檢測研究java


一 分析視頻數據中車輛的振動和光影變化

1.1 邊緣檢測

import cv2
import os
import time
import torch.nn as nn
import torch
import numpy as np
import torchvision.transforms as transforms
import torchvision
from PIL import Image
from matplotlib import pyplot as plt

np.set_printoptions(threshold=np.inf)
# threshold表示: Total number of array elements to be print(輸出數組的元素數目)

cap1 = cv2.VideoCapture("static.mkv")  # 0 使用默認的電腦攝像頭
cap2 = cv2.VideoCapture("move.mkv")  # 0 使用默認的電腦攝像頭
while (True):
    # 1.獲取一幀幀圖像
    ret1, static = cap1.read()
    ret2, move = cap2.read()
    # 轉灰度圖
    static = cv2.cvtColor(static, cv2.COLOR_BGR2GRAY)
    move = cv2.cvtColor(move, cv2.COLOR_BGR2GRAY)
    # 肯定閾值
    threshold = 130
    # 閾值分割
    ret1, static = cv2.threshold(static, threshold, 255, cv2.THRESH_BINARY)
    ret2, move = cv2.threshold(move, threshold, 255, cv2.THRESH_BINARY)
    cv2.imshow('static', static)
    cv2.imshow('move', move)
    # 按下「q」鍵中止
    if cv2.waitKey(1) & 0xFF == ord('q'):  # cv2.waitKey(1) 1毫秒讀一次
        break
cap1.release()
cap2.release()
cv2.destroyAllWindows()

在這裏插入圖片描述

1.2 轉HSV查看亮度等變化

在這裏插入圖片描述
在這裏插入圖片描述


二 分析總結:

正面車輛振動和靜止狀態在震顫不明顯,除去振動,另外一方面,顏色光影變化在車輛振動時,也不明顯,同時隨着幀數的變化,攝像頭所拍視頻存在大面積輕微噪聲,基本覆蓋住了車輛振動形成的光影變化。python

從正面進行車輛的震顫檢測和光度變化等進行熄火檢測不現實,建議拍攝尾部視頻,進行排氣管震顫或者冒煙檢測數組

今日拍攝了車輛尾部視頻,總體尾部振動不明顯,排氣管振動不明顯,排出氣體基本透明,沒法檢測。app


三 結果:

檢測視頻車輛熄火,未成功!dom


四 研究過程當中的收穫:能夠對車輛進行檢測和分割

使 用 F a s t e r R C N N 進 行 車 輛 視 頻 檢 測 使用FasterRCNN進行車輛視頻檢測 使用FasterRCNN進行車輛視頻檢測ide

import cv2
import os
import time
import torch.nn as nn
import torch
import numpy as np
import torchvision.transforms as transforms
import torchvision
from PIL import Image
from matplotlib import pyplot as plt

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

COCO_INSTANCE_CATEGORY_NAMES = [
    '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
    'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
    'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
    'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
    'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
    'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
    'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
    'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
    'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
    'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
    'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]
cap = cv2.VideoCapture("move.mkv")  # 0 使用默認的電腦攝像頭


while (True):
    # 1.獲取一幀幀圖像
    ret, frame = cap.read()

    # 2.獲取模型
    model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
    model.eval()

    # 3.圖像送進模型
    preprocess = transforms.Compose([
        transforms.ToTensor(),
    ])
    # 3.1. preprocess
    img_chw = preprocess(frame)

    # 3.2 to device
    if torch.cuda.is_available():
        img_chw = img_chw.to('cuda')
        model.to('cuda')

    # 3.3 forward
    input_list = [img_chw]
    with torch.no_grad():
        tic = time.time()
        # print("input img tensor shape:{}".format(input_list[0].shape))
        output_list = model(input_list)
        output_dict = output_list[0]
        # print("pass: {:.3f}s".format(time.time() - tic))
        # for k, v in output_dict.items():
        #     print("key:{}, value:{}".format(k, v))

    # 3.4. visualization
    out_boxes = output_dict["boxes"].cpu()
    out_scores = output_dict["scores"].cpu()
    out_labels = output_dict["labels"].cpu()

    num_boxes = out_boxes.shape[0]
    max_vis = 2
    thres = 0.995


    for idx in range(0, min(num_boxes, max_vis)):

        score = out_scores[idx].numpy()  # 置信分數
        bbox = out_boxes[idx].numpy()  # 邊框座標
        class_name = COCO_INSTANCE_CATEGORY_NAMES[out_labels[idx]]  # 類別輸出

        if score < thres:
            continue
        frame = cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 3)
        print("座標:",(bbox[0], bbox[1]), (bbox[2], bbox[3]))
        loacation = str(((bbox[2]-bbox[0]),(bbox[3]-bbox[1])))
        frame = cv2.putText(frame,loacation, (int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.8, (0, 0, 0))

    cv2.imshow('frame', frame)
    # 按下「q」鍵中止
    if cv2.waitKey(1) & 0xFF == ord('q'):  # cv2.waitKey(1) 1毫秒讀一次
        break
cap.release()
cv2.destroyAllWindows()

在這裏插入圖片描述

使 用 M a s k e r R C N N 進 行 車 輛 視 頻 的 檢 測 和 分 割 使用MaskerRCNN進行車輛視頻的檢測和分割 使用MaskerRCNN進行車輛視頻的檢測和分割ui

import cv2
import os
import time
import torch.nn as nn
import torch
import numpy as np
import torchvision.transforms as transforms
import torchvision
from PIL import Image
from matplotlib import pyplot as plt
import random
# np.set_printoptions(threshold=np.inf)
# threshold表示: Total number of array elements to be print(輸出數組的元素數目)

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

COCO_INSTANCE_CATEGORY_NAMES = [
    '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
    'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
    'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
    'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
    'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
    'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
    'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
    'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
    'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
    'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
    'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]
cap = cv2.VideoCapture("move.mkv")  # 0 使用默認的電腦攝像頭

def random_colour_masks(image):
    colours = [[0, 255, 0], [0, 0, 255], [255, 0, 0], [0, 255, 255], [255, 255, 0], [255, 0, 255], [80, 70, 180],
               [250, 80, 190], [245, 145, 50], [70, 150, 250], [50, 190, 190]]
    r = np.zeros_like(image).astype(np.uint8)
    g = np.zeros_like(image).astype(np.uint8)
    b = np.zeros_like(image).astype(np.uint8)
    r[image == 1], g[image == 1], b[image == 1] = colours[random.randrange(0, 10)]
    coloured_mask = np.stack([r, g, b], axis=2)
    return coloured_mask


while (True):
    # 1.獲取一幀幀圖像
    ret, frame = cap.read()

    # 2.獲取模型
    model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
    model.eval()

    # 3.圖像送進模型
    preprocess = transforms.Compose([
        transforms.ToTensor(),
    ])
    # 3.1. preprocess
    img_chw = preprocess(frame)

    # 3.2 to device
    if torch.cuda.is_available():
        img_chw = img_chw.to('cuda')
        model.to('cuda')

    # 3.3 forward
    input_list = [img_chw]
    with torch.no_grad():
        tic = time.time()
        # print("input img tensor shape:{}".format(input_list[0].shape))
        output_list = model(input_list)
        output_dict = output_list[0]
        # print("pass: {:.3f}s".format(time.time() - tic))
        # for k, v in output_dict.items():
        #     print("key:{}, value:{}".format(k, v))

    # 3.4. visualization
    out_boxes = output_dict["boxes"].cpu()
    out_scores = output_dict["scores"].cpu()
    out_labels = output_dict["labels"].cpu()

    out_masks = output_dict["masks"].cpu()
    #print(out_masks[1].numpy())

    num_boxes = out_boxes.shape[0]
    max_vis = 40
    thres = 0.5
    masks = (output_dict["masks"] > 0.5).squeeze().detach().cpu().numpy()


    for i in range(len(masks)):
        rgb_mask = random_colour_masks(masks[i])
        frame = cv2.addWeighted(frame, 1, rgb_mask, 0.5, 0)


    # rgb_mask = random_colour_masks(masks[])
    # frame = cv2.addWeighted(frame, 1, rgb_mask, 0.5, 0)
    # 下面的註釋解開,就是加上檢測
    # for idx in range(0, min(num_boxes, max_vis)):
    #
    #     score = out_scores[idx].numpy()  # 置信分數
    #     bbox = out_boxes[idx].numpy()  # 邊框座標
    #     class_name = COCO_INSTANCE_CATEGORY_NAMES[out_labels[idx]]  # 類別輸出
    #
    #     if score < thres:
    #         continue
    #     frame = cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 3)
    #     print("座標:",(bbox[0], bbox[1]), (bbox[2], bbox[3]))
    #     loacation = str(((bbox[2]-bbox[0]),(bbox[3]-bbox[1])))
    #     frame = cv2.putText(frame,loacation, (int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.8, (0, 0, 0))

    cv2.imshow('frame', frame)
    # 按下「q」鍵中止
    if cv2.waitKey(1) & 0xFF == ord('q'):  # cv2.waitKey(1) 1毫秒讀一次
        break
cap.release()
cv2.destroyAllWindows()

在這裏插入圖片描述

相關文章
相關標籤/搜索