<jupyter_start><jupyter_text>YOLO<jupyter_code>!python yolov3/train.py --freeze-layers --multi-scale --cache-images --data label/hcmcaic.data --cfg /content/hcmaic_tqker/label/yolov3-spp.cfg --weights /content/hcmaic_tqker/yolov3-spp-ultralytics.pt  --epochs 200
#Save model weights
!cp -r /content/hcmaic_tqker/weights_0908 '/content/drive/My Drive/HCMCAIC'

!python yolov3/detect.py --weights '/content/hcmaic_tqker/weights/best.pt' --cfg label/yolov3-spp.cfg --source '/content/hcmaic_tqker/test/image_1.jpeg' --names '/content/hcmaic_tqker/label/hcmcaic.names'<jupyter_output>Namespace(agnostic_nms=False, augment=False, cfg='label/yolov3-spp.cfg', classes=None, conf_thres=0.3, device='', fourcc='mp4v', half=False, img_size=512, iou_thres=0.6, names='/content/hcmaic_tqker/label/hcmcaic.names', output='output', save_txt=False, source='/content/hcmaic_tqker/test/image_1.jpeg', view_img=False, weights='/content/hcmaic_tqker/weights/best.pt')
Using CUDA device0 _CudaDeviceProperties(name='Tesla K80', total_memory=11441MB)

Model Summary: 225 layers, 6.25895e+07 parameters, 6.25895e+07 gradients
image 1/1 /content/hcmaic_tqker/test/image_1.jpeg: 320x512 2 Loai 1s, 3 Loai 2s, Done. (0.082s)
Results saved to /content/hcmaic_tqker/output
Done. (0.220s)<jupyter_text>EfficientDet<jupyter_code>!git clone https://github.com/xuannianz/EfficientDet.git
%cd EfficientDet
!pip install -r requirements.txt
!cp '/content/drive/My Drive/EfficientDet/csv_10_0.1103_0.3017.h5' /content/weights
!unzip dataset.zip
!python train.py --snapshot-path '/content/drive/My Drive/EfficientDet' --phi 1 --snapshot '/content/weights/csv_50_0.0533_0.4322.h5'  --gpu 0 --random-transform --compute-val-loss --freeze-bn --batch-size 4 --steps 200 --epochs 100 csv "/content/EfficientDet/data/train.csv"  "/content/EfficientDet/data/class_id.csv" --val-annotations-path "/content/EfficientDet/data/valid.csv"
!cp -r '/content/drive/My Drive/HCMCAIC/hcmaic_tqker/videos' /content/test
#%%
import sys
import os
import cv2
import argparse
from tqdm import tqdm


#@markdown Your videos is stored in: 

input_dir='/content/test/videos'

#@markdown Frames extracted from videos will be stored in:
output_dir='/content/test/images'


video_paths = []
for r, d, f in os.walk(input_dir):
    for file in f:
        if '.mp4' in file:
            video_paths.append(os.path.join(r, file))


for video_path in video_paths:
    print(video_path)



for video_path in video_paths:
    video_dir_path = os.path.join(output_dir, os.path.splitext(os.path.basename(video_path))[0])
    if not os.path.isdir(video_dir_path):
        os.makedirs(video_dir_path)

    vid_cap = cv2.VideoCapture(video_path)
    num_frms, original_fps = int(vid_cap.get(cv2.CAP_PROP_FRAME_COUNT)), vid_cap.get(cv2.CAP_PROP_FPS)
## Number of skip frames
    time_stride = 1

    for frm_id in tqdm(range(0, num_frms, time_stride)):
        vid_cap.set(cv2.CAP_PROP_POS_FRAMES, frm_id)
        _, im = vid_cap.read()

        cv2.imwrite(os.path.join(video_dir_path, str(frm_id) + '.jpg'), im)

!python inference.py
!zip -r output_efficientdet.zip /content/output<jupyter_output>adding: content/output/ (stored 0%)
  adding: content/output/333.jpg (deflated 1%)
  adding: content/output/900.jpg (deflated 1%)
  adding: content/output/616.jpg (deflated 1%)
  adding: content/output/94.jpg (deflated 1%)
  adding: content/output/1180.jpg (deflated 1%)
  adding: content/output/645.jpg (deflated 4%)
  adding: content/output/89.jpg (deflated 1%)
  adding: content/output/739.jpg (deflated 1%)
  adding: content/output/483.jpg (deflated 1%)
  adding: content/output/425.jpg (deflated 2%)
  adding: content/output/171.jpg (deflated 1%)
  adding: content/output/668.jpg (deflated 2%)
  adding: content/output/50.jpg (deflated 1%)
  adding: content/output/1164.jpg (deflated 4%)
  adding: content/output/180.jpg (deflated 1%)
  adding: content/output/241.jpg (deflated 1%)
  adding: content/output/564.jpg (deflated 1%)
  adding: content/output/1033.jpg (deflated 1%)
  adding: content/output/504.jpg (deflated 1%)
  adding: content/output/970.jpg (deflated 1%)
  adding: content/outp[...]<jupyter_text>EfficientDet AUTOML<jupyter_code>!git clone https://github.com/google/automl.git
cd automl/efficientdet/
# Download backbone checkopints.
!wget https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco/efficientdet-d0.tar.gz
!tar xf efficientdet-d0.tar.gz

!rm -r /content/drive/My\ Drive/weights
!pip install -r requirements.txt
!unzip dataset_camera.zip
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 

!python main.py --mode=train_and_eval \
    --training_file_pattern=custom_tfrecord/train.record \
    --validation_file_pattern=custom_tfrecord/valid.record \
    --model_name=efficientdet-d0 \
    --model_dir='/content/drive/My Drive/automl_checkpoint'  \
    --ckpt=efficientdet-d0  \
    --train_batch_size=64 \
    --eval_batch_size=64 --eval_samples=1024 \
    --num_examples_per_epoch=5717 --num_epochs=200  \
    --hparams=/content/automl/efficientdet/custom_tfrecord/custom_config.yaml

import dataloader
input_fn=dataloader.InputReader('custom_tfrecord/train.record',
            is_training=True,
            use_fake_data=False,
            max_instances_per_image=100)
import tensorflow as tf

for example in  tf.compat.v1.python_io.tf_record_iterator("/home/vinh/Python/tfrecords/valid.record"):
    print(tf.train.Example.FromString(example))

!wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
!tar xf VOCtrainval_11-May-2012.tar
!mkdir tfrecord
!PYTHONPATH=".:$PYTHONPATH"  python dataset/create_pascal_tfrecord.py  \
    --data_dir=VOCdevkit --year=VOC2012  --output_path=tfrecord/pascal

# Download backbone checko--2020-08-12 10:44:20--  https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco/.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)... 192.168.127.12, 172.16.31.10, 192.168.127.12, ...
Connecting to storage.googleapis.com (storage.googleapis.com)|192.168.127.12|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2020-08-12 10:44:21 ERROR 404: Not Found.
pints.
!wget https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco/efficientdet-d0.tar.gz
!tar xf efficientdet-d0.tar.gz

!python main.py --mode=train_and_eval \
    --training_file_pattern=tfrecord/pascal*.tfrecord \
    --validation_file_pattern=tfrecord/pascal*.tfrecord \
    --model_name=efficientdet-d0 \
    --model_dir=/tmp/efficientdet-d0-finetune  \
    --ckpt=efficientdet-d0  \
    --train_batch_size=64 \
    --eval_batch_size=64 --eval_samples=1024 \
    --num_examples_per_epoch=5717 --num_epochs=50  \
    --hparams=voc_config.yaml
# Step 1: export saved model.
!python model_inspect.py --runmode=saved_model \
  --model_name=efficientdet-d0 --ckpt_path=efficientdet-d0 \
  --hparams="image_size=1920x1280" \
  --saved_model_dir=/tmp/saved_model

!python model_inspect.py --runmode=saved_model_infer \
  --model_name=efficientdet-d0  \
  --saved_model_dir=/tmp/saved_model  \
  --input_image=img.png --output_image_dir=/content/automl/efficientdet/tmp
# you can visualize the output /tmp/0.jpg<jupyter_output>2020-08-12 10:59:21.925483: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
2020-08-12 10:59:23.672563: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX512F
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-08-12 10:59:23.677717: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 2000185000 Hz
2020-08-12 10:59:23.677927: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x3060840 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-12 10:59:23.677959: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-08-12 10:59:23.679891: I tensorflow/stream_executor/platform/default/dso_loa[...]<jupyter_text>YOLOV5<jupyter_code>!git clone https://github.com/ultralytics/yolov5.git
%cd yolov5
!rm -r /content/yolov5/dataset
!pip install -r requirements.txt
!pip install -U PyYAML

!unzip dataset.zip
!cp '/content/drive/My Drive/HCMCAIC/yolov5_models/yolov5x.pt' 'weights'
!cp -r '/content/drive/My Drive/output_yolov5/runs' '/content/yolov5/'
!rm -r /content/yolov5/runs
import torch
torch.cuda.get_device_name(0)
!python train.py --logdir '/content/drive/My Drive/HCMCAIC/output_yolov5/runs' --multi-scale --img 640 --device '0' --batch 4 --epochs 300 --data '/content/yolov5/custom_traffic.yaml' --cfg ./models/yolov5x.yaml --weights '/content/yolov5/weights/yolov5x.pt'

!python train.py --resume --device '0' --multi-scale --img 640 --batch 8 --epochs 5 --data '/content/yolov5/custom_traffic.yaml' --cfg ./models/yolov5x.yaml --weights '/content/drive/My Drive/HCMCAIC/output_yolov5/runs/exp0/weights/last.pt'

!cp -r '/content/yolov5/runs' '/content/drive/My Drive/output_yolov5'

!mkdir /content/hcmaic_tqker/yolov5/video
!cp '/content/drive/My Drive/HCMCAIC/aic-hcmc2020/videos/cam_19.mp4' /content/hcmaic_tqker/yolov5/video

%cd /content/video

import os
for i in range(19,20):
  name = 'cam_{}.mp4'.format(i)
  print(name)
  os.system("cp /content/drive/My\ Drive/HCMCAIC/aic-hcmc2020/videos/{} /content/video".format(name))

!cp '/content/drive/My Drive/HCMCAIC/aic-hcmc2020/zones-movement_paths.zip' /content/video
!unzip zones-movement_paths.zip
%cd /content/hcmaic_tqker/yolov5
!rm -r /content/yolov5/inference/output
%cd /content/yolov5
#%%
import sys
import os
import cv2
import argparse
from tqdm import tqdm


#@markdown Your videos is stored in: 

input_dir='/content/video'

#@markdown  Frames extracted from videos will be stored in:
output_dir='/content/yolov5/inference/test' 


video_paths = []
for r, d, f in os.walk(input_dir):
    for file in f:
        if '.mp4' in file:
            video_paths.append(os.path.join(r, file))


for video_path in video_paths:
    print(video_path)



for video_path in video_paths:
    video_dir_path = os.path.join(output_dir, os.path.splitext(os.path.basename(video_path))[0])
    if not os.path.isdir(video_dir_path):
        os.makedirs(video_dir_path)

    vid_cap = cv2.VideoCapture(video_path)
    num_frms, original_fps = int(vid_cap.get(cv2.CAP_PROP_FRAME_COUNT)), vid_cap.get(cv2.CAP_PROP_FPS)

## Number of skip frames
    time_stride = 1

    for frm_id in tqdm(range(0, num_frms, time_stride)):
        vid_cap.set(cv2.CAP_PROP_POS_FRAMES, frm_id)
        _, im = vid_cap.read()

        cv2.imwrite(os.path.join(video_dir_path, str(frm_id) + '.jpg'), im)
!cp  '/content/drive/My Drive/HCMCAIC/output_yolov5/runs/exp0/weights/best.pt' weights/
!pip install filterpy scikit-image lap
!pip install -r requirements.txt

import numpy as np
a = [1, 0]
b = [4, 1]
np.dot(a, b)

%cd /content/yolov5
!python detect.py --source /content/yolov5/inference/test/cam_19 --weights 'weights/best.pt' --conf 0.4
!zip -r '/content/drive/My Drive/HCMCAIC/sample_19_track.zip' /content/hcmaic_tqker/yolov5/inference/output
import os
from getpass import getpass
import urllib

user = input('User name: ')
password = getpass('Password: ')
password = urllib.parse.quote(password) # your password is converted into url format

cmd_string = 'git clone https://{0}:{1}@github.com/huylg/hcmaic_tqker.git --recursive'.format(user, password)

os.system(cmd_string)
cmd_string, password = "", "" # removing the password from the variable
%cd /content/hcmaic_tqker/yolov5/
!git checkout master
%cd /content/hcmaic_tqker/yolov5/video

import os
# for i in os.listdir('/content/drive/My Drive/HCMCAIC/aic-hcmc2020/videos'):
#   print(i)

for i in range(19,20):
  name = 'cam_{}.mp4'.format(i)
  print(name)
  os.system("cp /content/drive/My\ Drive/HCMCAIC/aic-hcmc2020/videos/{} /content/hcmaic_tqker/yolov5/video".format(name))


# for i in os.listdir('/content/drive/My Drive/HCMCAIC/aic-hcmc2020/videos'):
#   name = 'cam_0{}.mp4'.format(i)
#   print(i)
#   os.system("cp /content/drive/My\ Drive/HCMCAIC/aic-hcmc2020/videos/{} /content/hcmaic_tqker/yolov5/video".format(i))
  

!cp '/content/drive/My Drive/HCMCAIC/aic-hcmc2020/zones-movement_paths.zip' /content/hcmaic_tqker/yolov5/video
!unzip zones-movement_paths.zip
%cd /content/hcmaic_tqker/yolov5
import sys
import os
import cv2
import argparse
from tqdm.notebook import tqdm
from detect import *
from sort import *
import json
import bb_polygon
def load_zone_anno(json_filename):
  """
  Load the json with ROI and MOI annotation.
  """
  with open(json_filename) as jsonfile:
    dd = json.load(jsonfile)
    polygon = [(int(x), int(y)) for x, y in dd['shapes'][0]['points']]
    paths = {}
    for it in dd['shapes'][1:]:
      kk = str(int(it['label'][-2:]))
      paths[kk] = [(int(x), int(y)) for x, y
              in it['points']]
  return polygon, paths

def check_bbox_intersect_polygon(polygon, bbox):
  """
  
  Args:
    polygon: List of points (x,y)
    bbox: A tuple (xmin, ymin, xmax, ymax)
  
  Returns:
    True if the bbox intersect the polygon
  """
  x1, y1, x2, y2 = bbox
  bb = [(x1,y1), (x2, y1), (x2,y2), (x1,y2)]
  return bb_polygon.is_bounding_box_intersect(bb, polygon)

def get_track_dict(trackers,frame_id,track_dict):
  for xmin, ymin, xmax, ymax, track_id in trackers:
    track_id = int(track_id)
    if track_id not in track_dict:
      track_dict[track_id] = [(xmin, ymin, xmax, ymax, frame_id)]
    else:
      track_dict[track_id].append((xmin, ymin, xmax, ymax, frame_id))
  return track_dict
def bb_intersection_over_union(boxA, boxB):
	# determine the (x, y)-coordinates of the intersection rectangle
	xA = max(boxA[0], boxB[0])
	yA = max(boxA[1], boxB[1])
	xB = min(boxA[2], boxB[2])
	yB = min(boxA[3], boxB[3])
	# compute the area of intersection rectangle
	interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
	# compute the area of both the prediction and ground-truth
	# rectangles
	boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
	boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
	# compute the intersection over union by taking the intersection
	# area and dividing it by the sum of prediction + ground-truth
	# areas - the interesection area
	iou = interArea / float(boxAArea + boxBArea - interArea)
	# return the intersection over union value
	return iou

from utils.general import plot_one_box
def draw_bbox(trackers,img,classId):
  for xmin, ymin, xmax, ymax, track_id in trackers:
    xyxy = [xmin, ymin, xmax, ymax]
    label = "{}+{}".format(track_id, classId)
    plot_one_box(xyxy, img, label=label, line_thickness=3)

    
!rm -r /content/hcmaic_tqker/yolov5/inference/output
!mkdir /content/hcmaic_tqker/yolov5/inference/output
#@markdown Your videos is stored in: 
input_video_dir='/content/hcmaic_tqker/yolov5/video'
pathJson = '/content/hcmaic_tqker/yolov5/video/zones-movement_paths'
weight_path = 'weights/yolov5x.pt'
#@markdown  Frames extracted from videos will be stored in:
output_submit_dir='/content/output'
video_paths = []
for r, d, f in os.walk(input_video_dir):
    for file in f:
        if '.mp4' in file:
            video_paths.append(os.path.join(r, file))


for video_path in video_paths: 
    print(video_path)


opt = Option(weight_path,conf_thres=0.4,output_path=output_submit_dir,device='0')
print(opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size)
detector_tmp = detector(opt)

video_data_dict = {}

classIdArr = [0,1,2,3,5,7]

for video_path in video_paths:
  vid_cap = cv2.VideoCapture(video_path)
  fileNameVideo = os.path.split(video_path)[1][:-4]
  num_frms, original_fps = int(vid_cap.get(cv2.CAP_PROP_FRAME_COUNT)), vid_cap.get(cv2.CAP_PROP_FPS)
  polygon, paths = load_zone_anno(os.path.join(pathJson,'{}.json'.format(fileNameVideo)))
  print("Zone_Path:", os.path.join(pathJson,'{}.json'.format(fileNameVideo)))
  time_stride = 1
  #Define tracker
  tracker_l1 = Sort(max_age=10,min_hits=3)
  track_dict_l1={}

  tracker_l2 = Sort(max_age=10,min_hits=3)
  track_dict_l2={}

  tracker_l3 = Sort(max_age=10,min_hits=3)
  track_dict_l3={}

  tracker_l4 = Sort(max_age=10,min_hits=3)
  track_dict_l4={}

  for frm_id in tqdm(range(0, num_frms, time_stride)):
      vid_cap.set(cv2.CAP_PROP_POS_FRAMES, frm_id)
      _, opt.source = vid_cap.read()
      pred = detector_tmp.detect(opt)
      dets = []
      persons = []
      vehicle_l1 = []
      for bbox, classId, score in pred:
        # classIdArr
        if classId in classIdArr:
          if check_bbox_intersect_polygon(polygon, (bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1])):
            if classId == 0:
              persons.append((frm_id, 0, bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1], float(score)))
            if classId in [1,3]:
              vehicle_l1.append((frm_id, 0, bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1], float(score)))
            if classId == 2:
              dets.append((frm_id, 1, bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1], float(score)))
            if classId == 5:
              dets.append((frm_id, 2, bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1], float(score)))
            if classId == 7:
              dets.append((frm_id, 3, bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1], float(score)))
      for person in persons:
        check = False
        bboxA = person[2:-1]
        for vehicle in vehicle_l1:
          bboxB = vehicle[2:-1]
          if bb_intersection_over_union(bboxB,bboxA) > 0.1:
            check = True
            dets.append(person)
            break
        if check == False:  
          dets.append(person)
      dets = np.array(dets)
      if dets.size > 0:
        dets_l1 = np.array(dets[dets[:,1]==0])[:,2:]
        dets_l2 = np.array(dets[dets[:,1]==1])[:,2:]
        dets_l3 = np.array(dets[dets[:,1]==2])[:,2:]
        dets_l4 = np.array(dets[dets[:,1]==3])[:,2:]
        # print(dets_l1)
        trackers_1 = tracker_l1.update(dets_l1)
        trackers_2 = tracker_l2.update(dets_l2)
        trackers_3 = tracker_l3.update(dets_l3)
        trackers_4 = tracker_l4.update(dets_l4)
        
        # print("frm_id, detsL1, trkL1: ",frm_id,len(dets_l1),len(trackers_1))
        draw_bbox(trackers_1,opt.source,"1")
        cv2.imwrite('/content/hcmaic_tqker/yolov5/inference/output/{}.jpg'.format(frm_id),opt.source)
#--------------------------------------------------------------------------------------
  # video_data_dict[fileNameVideo] = [track_dict_l1]
  # video_data_dict[fileNameVideo].append(track_dict_l2)
  # video_data_dict[fileNameVideo].append(track_dict_l3)
  # video_data_dict[fileNameVideo].append(track_dict_l4)<jupyter_output>Using CUDA device0 _CudaDeviceProperties(name='Tesla K80', total_memory=11441MB)
{'fraction_non_alphanumeric': 0.14600973751058424, 'fraction_numerical': 0.04413632514817951, 'mean_word_length': 3.9598425196850395, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '181122', 'license': 'MIT', 'n_tokens_mistral': 7797, 'n_tokens_neox': 7294, 'n_words': 1353, 'path': 'notebooks/EfficientDet.ipynb', 'repo_name': 'vantuan5644/ReceiptOCR', 'size': '18899'}