AWS EC 2で火災検知項目を実行


ビデオ分析サーバを実装する前に、まず火災探知項目を真似して、感覚を探します.
難関.
私の古いノートパソコンcpuは老朽化しています.X以降は実行されません.awsからパソコンを借りることにしました.しかし、アマゾンはコマンドラインインタフェースであり、画像やビデオをアップロードすることで実際の効果を観察することはできません.気がふさいでXmingをインストールして、優奮闘デスクトップをインストールした後、ウィンドウで遠隔接続してみましたが、2つともぼろぼろで、いつも弾いていました.フレームを印刷して、監視カメラをリアルタイムで受信するかどうかを確認し、検出したら保存して見ます.
困難.
まだ防犯カメラがないので、残りのスマホをWebカメラにして、リアルタイムで動画を受信することにしました.しかし、http://フォーマットで始まったストリーミングビデオは、優奮闘からopencvまで読まれなかった.念のため、パブリックデータポータルサイトで公開されているrtsp://フォーマットはよく受け入れられています.夜の照明もランプで識別できるので、交通cctvのビデオを火災感知モデルに持ち込むことにしました.
難関.
火災感知cctvプロジェクトは3年前のコードなので、多くの関数が使用されていません.エラーは毎回グーグルで解決します.
シンプルなコードでアンドロイド接続とcctvストレージを排除
import cv2
import os
import Constants
import queue
import threading
import datetime
import sys
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import time


def init():
    global img_file_path, frame_queue, fourcc
    print("init")
    frame_queue = queue.Queue() #프레임 큐
    fourcc = cv2.VideoWriter_fourcc(*'XVID') #포맷 
    
def set_video_writer(video_name, fourcc, FPS, width, height):
    return cv2.VideoWriter(video_name, fourcc, FPS, (width, height))

#CCTV 영상 실시간으로 가져와서 프레임 큐에 저장
def connect_cctv():
    global frame_queue,fource, cur_frame
    
    url = 'rtsp://210.99.70.120:1935/live/cctv001.stream' #공공 데이터 센터에서 제공한 공공 CCTV
    
    try:
        cap = cv2.VideoCapture(url)
        print("연결성공: ", url)
    except:
        print("연결 실패")
        
    while True:
        ret, frame = cap.read()
        #전역 변수 프레임 큐에 프레임 담음 
        if frame_queue.qsize() < Constants.QUEUE_SIZE:
            frame_queue.put(frame)
        else:
            frame_queue.get()
            frame_queue.put(frame)


#실시간으로 화재 감지 
def run_detector():
    global frame_queue
    print("run_detector")
    
    from utils import label_map_util
    from utils import visualization_utils as vis_util

    sys.path.append("..")  
    
    MODEL_NAME = 'inference_graph'
 
    CWD_PATH = os.getcwd()     # 현재 경로
    PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')     # 모델 경로
    PATH_TO_LABELS = os.path.join(CWD_PATH,'training','label_map.pbtxt')     # pbtext 파일 
    NUM_CLASSES = 1

    #라벨링 맵 로드
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # 텐서플로우 모델 로드
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.compat.v1.GraphDef()
        with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
        sess = tf.compat.v1.Session(graph=detection_graph)
        
    #인풋
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # 아웃풋
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')     # 박스 
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')     #점수  
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')   
    num_detections = detection_graph.get_tensor_by_name('num_detections:0') 
    
    print(detection_scores, num_detections)
    
    while True:
        if frame_queue.empty() == True:
            continue
        image = frame_queue.get()       
        image_expanded = np.expand_dims(image, axis=0)

        (boxes, scores, classes, num) = sess.run(
            [detection_boxes, detection_scores, detection_classes, num_detections],
            feed_dict={image_tensor: image_expanded})
           
  
        # 박스, 라벨 표시 
        det_ret_list = vis_util.visualize_boxes_and_labels_on_image_array(
            image,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.80)

        #불 감지 
        if 0 < len(det_ret_list):
            for det_ret in det_ret_list:
                cats_list = det_ret["cats"]
                for cats_str in cats_list: 
                    splt_cat = cats_str.split(":")
                    print("불 감지됨, 감지시간: ", time.strftime('%H시%M분%S초'))
                    cv2.imwrite(os.path.join('fire',  time.strftime('%H시%M분%S초') + '.jpg'), image)   
       
        else:
            print("감지 안됨")
            continue
                          
if __name__ == "__main__":
    init()
    t1 = threading.Thread(target=connect_cctv)
    t1.start()
    run_detector()
    t1.join()
今後の任務
  • という例では1台のcctvしか分析できず、10台のcctvがあれば大量の資源を消費するが、すでに費用が心配されている.機械学習に特化したクラウドサービスを理解する必要があります.
  • 各cctvは、各スマートフォンに適切な注意と制御をどのように送信するか分からない.ソケットプログラミングの本をもう一度読みます.