資源簡介
安全帽檢測的檢測,使用python實現,僅供參考。
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
import?threadpool
import?threading
import?time
import?random
import?Queue
import?numpy?as?np
import?cv2
import?sys
import?osshutil
import?time
import?datetime
from?imutils.object_detection?import?non_max_suppression
from?imutils?import?paths
import?imutils???#安裝庫pip?install?imutils?;pip?install?--upgrade?imutils更新版本大于v0.3.1
import?threading
#import?get_video_address
import?copy
import?hashlib
from?imutils.video?import?FileVideoStream
from?imutils.video?import?VideoStream
class?bug_rect_info:
????def?__init__(selfxywh):
????????self.x?=?x
????????self.y?=?y
????????self.w?=?w
????????self.h?=?h
????????self.count?=?1
class?people_detect:
????def?__init__(self):
????????#已經訓練好的分類器,檢測佩戴安全頭的人員
????????self.face_cascade?=?cv2.CascadeClassifier(os.path.join(sys.path[0]?‘cascade-v2.5.xml‘))
????????#檢測行人
????????self.hog?=?cv2.HOGDescriptor()???#初始化方向梯度直方圖描述子
????????self.hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())??#設置支持向量機(Support?Vector?Machine)使得它成為一個預先訓練好了的行人檢測器
????????self.font?=?cv2.FONT_HERSHEY_SIMPLEX
????????self.IsExit?=?False
????????self.dir_temp?=?os.path.join(sys.path[0]?“temp“)
????????self.dir_people?=?os.path.join(sys.path[0]?“people“)
????????self.dir_hat?=?os.path.join(sys.path[0]?“hat“)
????????self.dir_empty?=?os.path.join(sys.path[0]?“empty“)
????????self.dir_head?=?os.path.join(sys.path[0]?“head“)
????????self.bug_list?=?[]
????????#創建臨時目錄
????????if?not?os.path.exists(self.dir_temp):?os.makedirs(self.dir_temp)
????????if?not?os.path.exists(self.dir_people):?os.makedirs(self.dir_people)
????????if?not?os.path.exists(self.dir_hat):?os.makedirs(self.dir_hat)
????????if?not?os.path.exists(self.dir_temp):?os.makedirs(self.dir_temp)
????????if?not?os.path.exists(self.dir_empty):?os.makedirs(self.dir_empty)
????????if?not?os.path.exists(self.dir_head):?os.makedirs(self.dir_head)
????#獲取視頻流的地址
????def?getframes(self):
????????#工地攝像頭
????????#capture?=?cv2.VideoCapture(get_video_address.get_video_url())
????????#本地攝像頭
????????#capture?=?cv2.VideoCapture(0)
????????#本地文件
????????capture?=?FileVideoStream(os.path.join(sys.path[0]‘test01.ts‘)).start()
????????#print(capture.isOpened())
????????return?capture
????#獲取行人
????def?get_people(selfframesmall_frame):
????????#檢測行人
????????(rects?weights)?=?self.hog.detectMultiScale(small_frame?winStride=(8?8)?padding=(8?8)?scale=1.15)
????????rects?=?np.array([[x?y?x?+?w?y?+?h]?for?(x?y?w?h)?in?rects])
????????index?=?0
????????offset_x?=?float(frame.shape[0])?/?float(small_frame.shape[0])
????????offset_y?=?float(frame.shape[1])?/?float(small_frame.shape[1])
????????print?offset_xoffset_y
????????head?=?datetime.datetime.now().strftime(‘%Y_%m_%d_%H_%M_%S‘)
????????for?(xywh)?in?rects:
????????????if?weights[index]?<=?0.68:?continue
????????????#檢測安全帽
????????????face?=?self.get_safe_hat(small_frame[y:hx
評論
共有 條評論