Skip to content

Commit

Permalink
3rd update
Browse files Browse the repository at this point in the history
webview 테스트를 위한 코드가 추가되었습니다.
  • Loading branch information
WhiteKIM committed Nov 3, 2022
1 parent 699441f commit c49afde
Show file tree
Hide file tree
Showing 500 changed files with 139,525 additions and 94 deletions.
Binary file modified __pycache__/custom_bridge_wrapper.cpython-310.pyc
Binary file not shown.
Binary file modified __pycache__/detect_deepsort.cpython-310.pyc
Binary file not shown.
38 changes: 19 additions & 19 deletions custom_bridge_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@
import json
import socketio
import base64

from torch.multiprocessing import Process
from flask import jsonify

#host = 'http://192.168.1.37:5000'
host = 'http://127.0.0.1:25000'

encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]

#sio = socketio.Client()
#sio.connect(host)
sio = socketio.Client()
sio.connect(host)

# load configuration for object detector
config = ConfigProto()
Expand All @@ -57,10 +58,13 @@ def __init__(self, name, index, cam, x, y, z, w):
self.w = w

def getLocation(self):
return self.x, self.y, self.z, self.w
return [self.x, self.y, self.z, self.w]

def getIndex(self):
return self.index

def getName(self):
return self.name+self.index

def updateLocation(self, x, y, z, w):
self.x = x
Expand Down Expand Up @@ -108,13 +112,17 @@ def __init__(self, reID_model_path:str, detector, max_cosine_distance:float=0.4,
self.coco_names_path = coco_names_path
self.nms_max_overlap = nms_max_overlap
self.class_names = read_class_names()

# initialize deep sort
self.encoder = create_box_encoder(reID_model_path, batch_size=1)
metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) # calculate cosine distance metric
self.tracker = Tracker(metric) # initialize tracker

def start(self):
pro1 = Process(target=self.track_video, args=()).start()
return self

def track_video(self,video:str, skip_frames:int=0, verbose:int = 0):
print('hello world')
'''
Track any given webcam or video
args:
Expand Down Expand Up @@ -234,20 +242,10 @@ def track_video(self,video:str, skip_frames:int=0, verbose:int = 0):

# 디버깅을 위한 함수입니다
# 현재 찾아낸 객체가 얼마나 존재하는지 확인하기 위한 코드입니다

'''
for model in modelList:
print(class_name +model.getIndex(), end='|')
print('\n')


'''
# 트랙이 업데이트 되는 것은 대략 1분가량
# 따라서 1분에 한번 데이터를 전송
if(time.time()-start > 60)
for model in modelList:
sendData = model.getJsonInfo()
connect.send(sendData.encode('utf-8'))
start = time()
'''

if verbose == 2:
Expand All @@ -273,14 +271,16 @@ def track_video(self,video:str, skip_frames:int=0, verbose:int = 0):
# output 영상을 웹상으로 띄어주는 코드
# 키보드 입력으로 q가 들어오면 종료됨
if True:
#cv2.imshow('output', output_video)
#cv2.imshow('output', result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
ret, buffer = cv2.imencode('.jpg', result, encode_param)
##ret, buffer = cv2.imencode('.jpg', result)
b64data = base64.b64encode(buffer)
#sio.emit('streaming', b64data)
## 스트리밍을 위해 데이터를 보내는 코드
sio.emit('streaming', b64data)
frame = buffer.tobytes()
yield frame
#output_video.release()
cv2.destroyAllWindows()
#sio.disconnect()
sio.disconnect()
32 changes: 20 additions & 12 deletions detect_deepsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
from json import JSONDecodeError
from shutil import which
from socket import socket
import threading
from detection_helpers import *
from tracking_helpers import *
from custom_bridge_wrapper import *
from PIL import Image
from flask import Flask, Response, render_template, jsonify, abort, redirect, url_for
#from flask_ngrok import run_with_ngrok
import datetime
from flask import Flask, Response, render_template, jsonify, abort, redirect, url_for, request
import threading

app = Flask(__name__)
app.debug = True
app.debug = False
app.use_reloader=False
app.threaded = True

tracker = None

@app.route('/')
def index():
"""Video streaming home page."""
now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %H:%M")
templateData = {
'title':'Image Streaming',
'time': timeString
'title':'Image Streaming'
}
return render_template('index.html', **templateData)
return render_template('index.html', **templateData, modelList = modelList)

# API를 통해 JSON을 전달할 함수
@app.route('/get', methods=["GET"])
Expand All @@ -38,14 +37,22 @@ def getAPI():
jsonString = json.loads(model.getJsonInfo())
jsonify['data'].append(jsonString)
return jsonify

def gen():
while True:
for frame in tracker.track_video(opt.source, skip_frames = 0, verbose=1):
yield b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n'
yield frame
yield b'\r\n'

@app.route('/update')
def updateHome():
return redirect('/')

def Tracking():
while True:
tracker.track_video()

# 테스트용 함수
# 웹상에서 연산을 수행한 결과값이 잘 나오는지를 확인하는 코드
@app.route('/video_feed')
Expand All @@ -61,6 +68,7 @@ def video_feed():
# Initialise class that binds detector and tracker in one class
tracker = YOLOv7_DeepSORT(reID_model_path="./deep_sort/model_weights/mars-small128.pb", detector=detector)
# output = None will not save the output video
app.run()
th1 = threading.Thread(target=tracker.track_video, args=(opt.source, 0, 1)).start()
app.run(threaded = True)


Loading

0 comments on commit c49afde

Please sign in to comment.