热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

关于物体(车辆)震颤(熄火)检测研究

关于物体(车辆)震颤(熄火)检测研究关于物体(车辆)震颤(熄火)检测

关于物体(车辆)震颤(熄火)检测研究关于物体(车辆)震颤(熄火)检测研究()()



一 分析视频数据中车辆的振动和光影变化


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 pltnp.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查看亮度等变化

在这里插入图片描述
在这里插入图片描述



二 分析总结:

正面车辆振动和静止状态在震颤不明显,除去振动,另一方面,颜色光影变化在车辆振动时,也不明显,同时随着帧数的变化,摄像头所拍视频存在大面积轻微噪声,基本覆盖住了车辆振动造成的光影变化。

从正面进行车辆的震颤检测和光度变化等进行熄火检测不现实,建议拍摄尾部视频,进行排气管震颤或者冒烟检测

今日拍摄了车辆尾部视频,整体尾部振动不明显,排气管振动不明显,排出气体基本透明,无法检测。



三 结果:

检测视频车辆熄火,未成功!



四 研究过程中的收获:可以对车辆进行检测和分割

使用FasterRCNN进行车辆视频检测使用FasterRCNN进行车辆视频检测使FasterRCNN

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 pltBASE_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 &#61; cv2.VideoCapture("move.mkv") # 0 使用默认的电脑摄像头while (True):# 1.获取一帧帧图像ret, frame &#61; cap.read()# 2.获取模型model &#61; torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained&#61;True)model.eval()# 3.图像送进模型preprocess &#61; transforms.Compose([transforms.ToTensor(),])# 3.1. preprocessimg_chw &#61; preprocess(frame)# 3.2 to deviceif torch.cuda.is_available():img_chw &#61; img_chw.to(&#39;cuda&#39;)model.to(&#39;cuda&#39;)# 3.3 forwardinput_list &#61; [img_chw]with torch.no_grad():tic &#61; time.time()# print("input img tensor shape:{}".format(input_list[0].shape))output_list &#61; model(input_list)output_dict &#61; 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. visualizationout_boxes &#61; output_dict["boxes"].cpu()out_scores &#61; output_dict["scores"].cpu()out_labels &#61; output_dict["labels"].cpu()num_boxes &#61; out_boxes.shape[0]max_vis &#61; 2thres &#61; 0.995for idx in range(0, min(num_boxes, max_vis)):score &#61; out_scores[idx].numpy() # 置信分数bbox &#61; out_boxes[idx].numpy() # 边框坐标class_name &#61; COCO_INSTANCE_CATEGORY_NAMES[out_labels[idx]] # 类别输出if score < thres:continueframe &#61; cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 3)print("坐标&#xff1a;",(bbox[0], bbox[1]), (bbox[2], bbox[3]))loacation &#61; str(((bbox[2]-bbox[0]),(bbox[3]-bbox[1])))frame &#61; cv2.putText(frame,loacation, (int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.8, (0, 0, 0))cv2.imshow(&#39;frame&#39;, frame)# 按下“q”键停止if cv2.waitKey(1) & 0xFF &#61;&#61; ord(&#39;q&#39;): # cv2.waitKey(1) 1毫秒读一次break
cap.release()
cv2.destroyAllWindows()

在这里插入图片描述

使用MaskerRCNN进行车辆视频的检测和分割使用MaskerRCNN进行车辆视频的检测和分割使MaskerRCNN

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&#61;np.inf)
# threshold表示: Total number of array elements to be print(输出数组的元素数目)BASE_DIR &#61; os.path.dirname(os.path.abspath(__file__))
device &#61; torch.device("cuda" if torch.cuda.is_available() else "cpu")COCO_INSTANCE_CATEGORY_NAMES &#61; [&#39;__background__&#39;, &#39;person&#39;, &#39;bicycle&#39;, &#39;car&#39;, &#39;motorcycle&#39;, &#39;airplane&#39;, &#39;bus&#39;,&#39;train&#39;, &#39;truck&#39;, &#39;boat&#39;, &#39;traffic light&#39;, &#39;fire hydrant&#39;, &#39;N/A&#39;, &#39;stop sign&#39;,&#39;parking meter&#39;, &#39;bench&#39;, &#39;bird&#39;, &#39;cat&#39;, &#39;dog&#39;, &#39;horse&#39;, &#39;sheep&#39;, &#39;cow&#39;,&#39;elephant&#39;, &#39;bear&#39;, &#39;zebra&#39;, &#39;giraffe&#39;, &#39;N/A&#39;, &#39;backpack&#39;, &#39;umbrella&#39;, &#39;N/A&#39;, &#39;N/A&#39;,&#39;handbag&#39;, &#39;tie&#39;, &#39;suitcase&#39;, &#39;frisbee&#39;, &#39;skis&#39;, &#39;snowboard&#39;, &#39;sports ball&#39;,&#39;kite&#39;, &#39;baseball bat&#39;, &#39;baseball glove&#39;, &#39;skateboard&#39;, &#39;surfboard&#39;, &#39;tennis racket&#39;,&#39;bottle&#39;, &#39;N/A&#39;, &#39;wine glass&#39;, &#39;cup&#39;, &#39;fork&#39;, &#39;knife&#39;, &#39;spoon&#39;, &#39;bowl&#39;,&#39;banana&#39;, &#39;apple&#39;, &#39;sandwich&#39;, &#39;orange&#39;, &#39;broccoli&#39;, &#39;carrot&#39;, &#39;hot dog&#39;, &#39;pizza&#39;,&#39;donut&#39;, &#39;cake&#39;, &#39;chair&#39;, &#39;couch&#39;, &#39;potted plant&#39;, &#39;bed&#39;, &#39;N/A&#39;, &#39;dining table&#39;,&#39;N/A&#39;, &#39;N/A&#39;, &#39;toilet&#39;, &#39;N/A&#39;, &#39;tv&#39;, &#39;laptop&#39;, &#39;mouse&#39;, &#39;remote&#39;, &#39;keyboard&#39;, &#39;cell phone&#39;,&#39;microwave&#39;, &#39;oven&#39;, &#39;toaster&#39;, &#39;sink&#39;, &#39;refrigerator&#39;, &#39;N/A&#39;, &#39;book&#39;,&#39;clock&#39;, &#39;vase&#39;, &#39;scissors&#39;, &#39;teddy bear&#39;, &#39;hair drier&#39;, &#39;toothbrush&#39;
]
cap &#61; cv2.VideoCapture("move.mkv") # 0 使用默认的电脑摄像头def random_colour_masks(image):colours &#61; [[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 &#61; np.zeros_like(image).astype(np.uint8)g &#61; np.zeros_like(image).astype(np.uint8)b &#61; np.zeros_like(image).astype(np.uint8)r[image &#61;&#61; 1], g[image &#61;&#61; 1], b[image &#61;&#61; 1] &#61; colours[random.randrange(0, 10)]coloured_mask &#61; np.stack([r, g, b], axis&#61;2)return coloured_maskwhile (True):# 1.获取一帧帧图像ret, frame &#61; cap.read()# 2.获取模型model &#61; torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained&#61;True)model.eval()# 3.图像送进模型preprocess &#61; transforms.Compose([transforms.ToTensor(),])# 3.1. preprocessimg_chw &#61; preprocess(frame)# 3.2 to deviceif torch.cuda.is_available():img_chw &#61; img_chw.to(&#39;cuda&#39;)model.to(&#39;cuda&#39;)# 3.3 forwardinput_list &#61; [img_chw]with torch.no_grad():tic &#61; time.time()# print("input img tensor shape:{}".format(input_list[0].shape))output_list &#61; model(input_list)output_dict &#61; 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. visualizationout_boxes &#61; output_dict["boxes"].cpu()out_scores &#61; output_dict["scores"].cpu()out_labels &#61; output_dict["labels"].cpu()out_masks &#61; output_dict["masks"].cpu()#print(out_masks[1].numpy())num_boxes &#61; out_boxes.shape[0]max_vis &#61; 40thres &#61; 0.5masks &#61; (output_dict["masks"] > 0.5).squeeze().detach().cpu().numpy()for i in range(len(masks)):rgb_mask &#61; random_colour_masks(masks[i])frame &#61; cv2.addWeighted(frame, 1, rgb_mask, 0.5, 0)# rgb_mask &#61; random_colour_masks(masks[])# frame &#61; cv2.addWeighted(frame, 1, rgb_mask, 0.5, 0)# 下面的注释解开&#xff0c;就是加上检测# for idx in range(0, min(num_boxes, max_vis)):## score &#61; out_scores[idx].numpy() # 置信分数# bbox &#61; out_boxes[idx].numpy() # 边框坐标# class_name &#61; COCO_INSTANCE_CATEGORY_NAMES[out_labels[idx]] # 类别输出## if score # continue# frame &#61; cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 3)# print("坐标&#xff1a;",(bbox[0], bbox[1]), (bbox[2], bbox[3]))# loacation &#61; str(((bbox[2]-bbox[0]),(bbox[3]-bbox[1])))# frame &#61; cv2.putText(frame,loacation, (int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.8, (0, 0, 0))cv2.imshow(&#39;frame&#39;, frame)# 按下“q”键停止if cv2.waitKey(1) & 0xFF &#61;&#61; ord(&#39;q&#39;): # cv2.waitKey(1) 1毫秒读一次break
cap.release()
cv2.destroyAllWindows()

在这里插入图片描述


推荐阅读
author-avatar
Mister-Sky_724
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有