<jupyter_start><jupyter_text>GPU Info<jupyter_code>!git clone https://github.com/jmhaussaire/SOAI-Project.git
import os
os.chdir('SOAI-Project')
!ls
!nvidia-smi<jupyter_output>Thu Feb 27 19:40:42 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.48.02    Driver Version: 418.67       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P100-PCIE...  Off  | 00000000:00:04.0 Off |                    0 |
| N/A   34C    P0    25W / 250W |      0MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU  [...]<jupyter_text>Imports<jupyter_code>%reload_ext autoreload
%autoreload 2
from os.path import join
from google.colab import drive
drive.mount('/gdrive')<jupyter_output>Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly

Enter your authorization code:
··········
Mounted at /gdrive<jupyter_text>Remount drive<jupyter_code>drive.mount("/gdrive", force_remount=True)
!pip install -q pascal-voc-writer
import matplotlib.pyplot as plt
from detectionprovider import DetectionProvider
from common import limit_gpu_memory, Detection, show_LOIs, show_LOI_Info
from IOUTracker import IOUTracker
from video import VideoStreamReader, VideoStreamWriter
from yolo import YOLO
from tqdm import tqdm_notebook
import numpy as np
limit_gpu_memory(0.7)
import cv2
from pascal_voc_writer import Writer as LabelWriter
import csv
import time
from datetime import datetime
import pandas as pd<jupyter_output><empty_output><jupyter_text>Parameters<jupyter_code>import os

# YOLO
yolo_score = 0.6
model_data_path = "/gdrive/My Drive/SOAI/TrafficAnalysis-master/model_data/yolo.h5"

# Inputs
data_path = "/gdrive/My Drive/SOAI/TrafficAnalysis-master/"
in_csv_name = os.path.join(data_path,'Vehicles_2016-05-10.csv')
startTime = pd.to_datetime('10.05.2016 11:53:16')
in_vid_name = os.path.join(data_path,"MAH00004.MP4")

# Outputs
out_folder = "/gdrive/My Drive/SOAI/outputs_27_02_2020/"
out_vid_name = os.path.join(out_folder,"900sec.mp4")
seconds_skip = 0  # number of seconds to skip
seconds_count = 10  # number of seconds to process
csv_out = os.path.join(out_folder,'Vehicle-list.csv')

timeInit = 42795.100<jupyter_output><empty_output><jupyter_text>Prepare YOLO network<jupyter_code>yolo = YOLO(score=yolo_score, model_path = model_data_path)<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:190: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:207: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:541: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4432: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:66: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

WARNING:tensorflow:Fr[...]<jupyter_text>Prepare CSV<jupyter_code>df = pd.read_csv(in_csv_name)
df['Start'] = pd.to_datetime(df["Start-Timestamp"],format='%m.%d.%Y %H:%M:%S')
df['End'] = pd.to_datetime(df["End-Timestamp"],format='%m.%d.%Y %H:%M:%S')
df_new =  df[df['Start'] >= startTime] 
df_new.reset_index(inplace=True)<jupyter_output><empty_output><jupyter_text>Run through frames<jupyter_code>%%time

vidreader = VideoStreamReader(
    in_vid_name, seconds_count=seconds_count, seconds_skip=seconds_skip, width=1920, height=1080)
vidwriter = VideoStreamWriter(
    out_vid_name, width=vidreader.width, height=vidreader.height, fps=vidreader.fps)
detection_provider = DetectionProvider(yolo)

# define the detection zone??
padding = 0
frame_bbox = Detection.from_frame(
    (vidreader.width, vidreader.height), int(padding))

tracker = IOUTracker()

csv_writer = True
videoShowLOI = True
LOIs = [[(450, 400), (600, 850)]]
ovwr = 0
vehicleInVid = 1.5 #when does the vehice first appears?
ovwrInfo = ('-','-','-','-')


# initialize .csv
with open(csv_out, 'w+') as f:
    writer = csv.writer(f)
    csv_line = 'timestamp,lane,vehicle,direction'
    writer.writerows([csv_line.split(',')])
# nice progress bar:
pbar = tqdm_notebook(total=vidreader.frame_count - vidreader.frame_skip)

# main loop
while True:
    frame = vidreader.next_frame()
    if frame is None:
        break
    imgName = "img_%s" % (vidreader.frame_no)
    XMLPathName = os.path.join(out_folder, imgName)
    imgPathName = os.path.join(out_folder, imgName)
    labelwriter = LabelWriter(XMLPathName, vidreader.width, vidreader.height)
    #yolo draws bounding boxes (bb) right into the image.
    #Since I want to be able to save images without bb, I have to make a copy:
    frame_copy = frame.copy()
    captureFrame = False
    captureCSV = False
    detected = False  
    pbar.update()
    
    #yolo detection: 
    detections = detection_provider.detect_boxes(frame, vidreader.frame_no)
    tracker.update(detections.copy(), frame_bbox)

    for track in tracker.active_tracks:
        track.likely_class()
        track.lane_detector()
        #lineDetections = track.csv_detector(LOIs)
        # checking for non-car vehicles that cross the "line of interest":
        for idx, val in enumerate(track.csv_detector(LOIs)):
            if val:
                #print("Detection on: ",idx)
                captureFrame = True
                captureCSV = True
                vTime = vidreader.capture.get(cv2.CAP_PROP_POS_MSEC) / 1000
                nowTime = startTime + \
                    pd.Timedelta(seconds=vTime) - \
                    pd.Timedelta(seconds=vehicleInVid)
                
                # look for a timestamp match in CSV file:
                timeRange = pd.Timedelta(seconds=0.7)
                matchingRows = df_new.loc[(df_new['Start'] > nowTime.floor('S') - timeRange) \
                    & (df_new['Start'] < nowTime.floor('S') + timeRange)]
                #matchingRows = df_new.loc[df_new['Start'].dt.floor(
                #    'S') == nowTime.floor('S')]
                if not matchingRows.empty:
                    probVeh = matchingRows['ExtCatText'].str.lower()
                    for vItem in probVeh.iteritems():
                        if vItem[1] != "car":
                            # overwrite vehicle class with data from CSV:
                            track.ovwrClass = vItem[1]
                    ovwr = vidreader.frame_no + 13
                    ovwrInfo = (track.detections[-1].object_class, track.ovwrClass, track.lane, str(nowTime.floor('S')))
                    #print("found match in csv")
                    
        track.show_history(frame)
        
        if captureCSV:
            if track.ovwrClass is not None:
                csv_line = str(nowTime) + "," + str(track.lane) + "," + \
                    str(track.ovwrClass) + "," + str(track.direction)
            elif track.res is not None:
                csv_line = str(nowTime) + "," + str(track.lane) + \
                    "," + str(track.res) + "," + str(track.direction)
            else:
                print("Hä?")
            with open(csv_out, 'a') as f:
                writer = csv.writer(f)
                (timestamp, lane, vehicle, direction) = csv_line.split(',')
                writer.writerows([csv_line.split(',')])
                #print("writing to csv")
            captureCSV = False

        if captureFrame or (ovwr > vidreader.frame_no and (vidreader.frame_no % 4 == 0)):
            dbox = track.detections[-1].to_tlbr()
            bx1 = dbox[0]
            by1 = dbox[1]
            bx2 = dbox[2]
            by2 = dbox[3]
            if track.res is None:
                print("track.res is None?!", track.res)
                #track.res = track.detections[-1].object_class
            if track.ovwrClass is not None:
                labelwriter.addObject(track.ovwrClass, bx1, by1, bx2, by2)
            elif track.res is not None:
                labelwriter.addObject(track.res, bx1, by1, bx2, by2)
            else:
                labelwriter.addObject("unknown", bx1, by1, bx2, by2)
            labelwriter.save(XMLPathName + ".xml")
            #print("write xml")

    if videoShowLOI:
        vTime = vidreader.capture.get(cv2.CAP_PROP_POS_MSEC) / 1000
        nowTime = startTime + \
                    pd.Timedelta(seconds=vTime) - \
                    pd.Timedelta(seconds=vehicleInVid)
        show_LOIs(frame, LOIs)        
        show_LOI_Info(frame,*ovwrInfo)
        
    
    vidwriter.write(frame)
    
    #save image every 4 frames if non-car is detected
    #or has been detected max. 13 frames before:
    if captureFrame or (ovwr > vidreader.frame_no and (vidreader.frame_no % 4 == 0)):
        cv2.imwrite(imgPathName + ".jpg",
                    cv2.cvtColor(frame_copy, cv2.COLOR_RGB2BGR))
        #print("save image")
        

pbar.close()
vidreader.release()
vidwriter.release()
print("job done")<jupyter_output><empty_output><jupyter_text>Functions<jupyter_code>def process_vid(in_vid_name,seconds_count,yolo,out_folder):
  """
  Look at every frame of the video and categorise the vehicles.
  Output an xml file listing the tracks, and a picture with the box.
  """
  %%time

  vidreader = VideoStreamReader(
      in_vid_name, seconds_count=seconds_count, seconds_skip=seconds_skip, width=1920, height=1080)
  vidwriter = VideoStreamWriter(
      out_vid_name, width=vidreader.width, height=vidreader.height, fps=vidreader.fps)
  detection_provider = DetectionProvider(yolo)

  # define the detection zone??
  padding = 0
  frame_bbox = Detection.from_frame(
      (vidreader.width, vidreader.height), int(padding))

  tracker = IOUTracker()

  csv_writer = True  
  ovwrInfo = ('-','-','-','-')


  # initialize .csv
  with open(csv_out, 'w+') as f:
      writer = csv.writer(f)
      csv_line = 'timestamp,vehicle,direction'
      writer.writerows([csv_line.split(',')])
  # nice progress bar:
  pbar = tqdm_notebook(total=vidreader.frame_count - vidreader.frame_skip)

  # main loop
  while True:
    frame = vidreader.next_frame()
    if frame is None:
        break
    imgName = "img_%s" % (vidreader.frame_no)
    XMLPathName = os.path.join(out_folder, imgName)
    imgPathName = os.path.join(out_folder, imgName)
    labelwriter = LabelWriter(XMLPathName, vidreader.width, vidreader.height)
    #yolo draws bounding boxes (bb) right into the image.
    #Since I want to be able to save images without bb, I have to make a copy:
    frame_copy = frame.copy()
    captureFrame = True
    captureCSV = True    
    pbar.update()
    
    #yolo detection: 
    detections = detection_provider.detect_boxes(frame, vidreader.frame_no)
    tracker.update(detections.copy(), frame_bbox)

    for track in tracker.active_tracks:
        track.likely_class()
        # I don't know yet the lane or the LOI
        # track.lane_detector()
        # lineDetections = track.csv_detector(LOIs)
        track.show_history(frame)
        
        vTime = vidreader.capture.get(cv2.CAP_PROP_POS_MSEC) / 1000
        nowTime = startTime + \
                  pd.Timedelta(seconds=vTime)

        if captureCSV:
            if track.ovwrClass is not None:
                csv_line = str(nowTime) + "," + \
                    str(track.ovwrClass) + "," + str(track.direction)
            elif track.res is not None:
                csv_line = str(nowTime) + "," + \
                     str(track.res) + "," + str(track.direction)
            else:
                print("Hä?")
            with open(csv_out, 'a') as f:
                writer = csv.writer(f)
                (timestamp, vehicle, direction) = csv_line.split(',')
                writer.writerows([csv_line.split(',')])
                #print("writing to csv")
            captureCSV = False

        if captureFrame:
            dbox = track.detections[-1].to_tlbr()
            bx1 = dbox[0]
            by1 = dbox[1]
            bx2 = dbox[2]
            by2 = dbox[3]
            if track.res is None:
                print("track.res is None?!", track.res)
                #track.res = track.detections[-1].object_class
            if track.ovwrClass is not None:
                labelwriter.addObject(track.ovwrClass, bx1, by1, bx2, by2)
            elif track.res is not None:
                labelwriter.addObject(track.res, bx1, by1, bx2, by2)
            else:
                labelwriter.addObject("unknown", bx1, by1, bx2, by2)
            labelwriter.save(XMLPathName + ".xml")
            #print("write xml")
    
    vidwriter.write(frame)
    
    #save image every 4 frames if non-car is detected
    #or has been detected max. 13 frames before:
    if captureFrame:
        # without bounding box
        # cv2.imwrite(imgPathName + ".jpg",
        #            cv2.cvtColor(frame_copy, cv2.COLOR_RGB2BGR))
        # with bounding box
        cv2.imwrite(imgPathName + ".jpg",
                    cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
        #print("save image")
        

  pbar.close()
  vidreader.release()
  vidwriter.release()
  print("job done")
process_vid(in_vid_name,seconds_count,yolo,out_folder)<jupyter_output>CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.25 µs
{'fraction_non_alphanumeric': 0.1378434065934066, 'fraction_numerical': 0.022939560439560438, 'mean_word_length': 2.452110004741584, 'pattern_counts': {'":': 2, '<': 30, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '485185', 'license': 'MIT', 'n_tokens_mistral': 4940, 'n_tokens_neox': 4613, 'n_words': 951, 'path': 'Swiss10_prep.ipynb', 'repo_name': 'jmhaussaire/SOAI-Project', 'size': '14560'}