<jupyter_start><jupyter_text>**SISTEMA DE INFORMACIÓN SOBRE LA OCUPACIÓN DE BUSES DEL TRANSPORTE PUBLICO.** El proyecto se enfoca en el desarrollo de un dispositivo inteligente, capaz de determinar el grado de ocupación de los buses de transporte público mediante el uso de técnicas de inteligencia artificial.    Primero configuramos TF versión 1 e instalamos `filterpy`<jupyter_code>%tensorflow_version 1.x
!pip install filterpy<jupyter_output>TensorFlow 1.x selected.
Collecting filterpy
[?25l  Downloading https://files.pythonhosted.org/packages/f6/1d/ac8914360460fafa1990890259b7fa5ef7ba4cd59014e782e4ab3ab144d8/filterpy-1.4.5.zip (177kB)
[K     |████████████████████████████████| 184kB 4.8MB/s 
[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from filterpy) (1.18.2)
Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from filterpy) (1.4.1)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from filterpy) (3.2.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib->filterpy) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->filterpy) (2.4.6)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->filterpy) (1.1.0)
Requirement already sa[...]<jupyter_text>Descargamos [Darkflow](https://github.com/thtrieu/darkflow://) de la implementación de YOLO y los pesos de la versión 2<jupyter_code>!git clone https://github.com/thtrieu/darkflow.git
%cd darkflow
!python setup.py build_ext --inplace
!pip install -e .
!pip install .
!mkdir bin
!wget  https://pjreddie.com/media/files/yolov2.weights -O bin/yolov2.weights
%cd ..<jupyter_output>Cloning into 'darkflow'...
remote: Enumerating objects: 2713, done.[K
remote: Total 2713 (delta 0), reused 0 (delta 0), pack-reused 2713[K
Receiving objects: 100% (2713/2713), 32.98 MiB | 27.66 MiB/s, done.
Resolving deltas: 100% (1762/1762), done.
/content/darkflow
Compiling darkflow/cython_utils/nms.pyx because it changed.
Compiling darkflow/cython_utils/cy_yolo2_findboxes.pyx because it changed.
Compiling darkflow/cython_utils/cy_yolo_findboxes.pyx because it changed.
[1/3] Cythonizing darkflow/cython_utils/cy_yolo2_findboxes.pyx
/usr/local/lib/python3.6/dist-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /content/darkflow/darkflow/cython_utils/cy_yolo2_findboxes.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
[2/3] Cythonizing darkflow/cython_utils/cy_yolo_findboxes.pyx
/usr/local/lib/python3.6/dist-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython [...]<jupyter_text>Creamos una copia de nuestro repositorio donde se encuentran algunas librerías necesarias y el People couting dataset [PCDS](https://freesoft.dev/program/128588362).<jupyter_code>!git clone https://gitlab.com/IsRaTiAl/v4c.git
!cp v4c/notebooks/sort.py darkflow/sort.py
!cp -r v4c/videos/test darkflow/test_videos<jupyter_output>Cloning into 'v4c'...
remote: Enumerating objects: 145, done.[K
remote: Counting objects: 100% (145/145), done.[K
remote: Compressing objects: 100% (97/97), done.[K
remote: Total 145 (delta 58), reused 119 (delta 42), pack-reused 0[K
Receiving objects: 100% (145/145), 288.74 MiB | 33.69 MiB/s, done.
Resolving deltas: 100% (58/58), done.<jupyter_text>Generamos algunos directorios que los emplearemos más adelante<jupyter_code>%cd darkflow/
!mkdir test_videos/front test_videos/front/crowd test_videos/front/uncrowd 
!mkdir test_videos/back test_videos/back/crowd test_videos/back/uncrowd 

!unrar e test_videos/crowd_day_back.rar test_videos/back/crowd
!unrar e test_videos/uncrowd_day_back.rar test_videos/back/uncrowd
!unrar e test_videos/crowd_day_front.rar test_videos/front/crowd
!unrar e test_videos/uncrowd_day_front.rar test_videos/front/uncrowd

!mkdir outputs/
!mkdir outputs/front outputs/front/crowd outputs/front/uncrowd 
!mkdir outputs/back outputs/back/crowd outputs/back/uncrowd
# !python flow --model cfg/yolo.cfg --load bin/yolov2.weights --demo 2015_05_08_08_27_20BackColor.avi --gpu 1.0 --saveVideo<jupyter_output><empty_output><jupyter_text>Importamos algunas las librerias `cv2`, `matplotlib`, `numpy` y `os` para la vision artificial, manejo de graficas, matrices y archivos respectivamente. Adicionalmente, importamos `TFNet` y `sort` para la detección y seguimiento de las personas.<jupyter_code>import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
from darkflow.net.build import TFNet
from sort import *<jupyter_output>WARNING:tensorflow:
The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
  * https://github.com/tensorflow/addons
  * https://github.com/tensorflow/io (for I/O related ops)
If you depend on functionality not listed there, please file an issue.

WARNING:tensorflow:From /content/darkflow/darkflow/net/build.py:15: The name tf.train.RMSPropOptimizer is deprecated. Please use tf.compat.v1.train.RMSPropOptimizer instead.

WARNING:tensorflow:From /content/darkflow/darkflow/net/build.py:16: The name tf.train.AdadeltaOptimizer is deprecated. Please use tf.compat.v1.train.AdadeltaOptimizer instead.

WARNING:tensorflow:From /content/darkflow/darkflow/net/build.py:17: The name tf.train.AdagradOptimizer is deprecated. Please use tf.compat.v1.train.AdagradOptimizer instead.

WARNING:tensorflow:From /content/darkflow/darkflow/net/build.py:18: The name tf.train.Ada[...]<jupyter_text>Instanciamos el objeto tfnet y le pasamos los parámetros  options, donde se especifican el modelo, los pesos, el umbral de detección y habilitamos el uso de la GPU.<jupyter_code>options = {"model": "cfg/yolo.cfg", "load": "bin/yolov2.weights", "threshold": 0.20, 'gpu':1}
tfnet = TFNet(options)

# imgcv = cv2.imread("f2.jpg")
# result = tfnet.return_predict(imgcv)
# print(result)<jupyter_output>Parsing cfg/yolo.cfg
Loading bin/yolov2.weights ...
Successfully identified 203934260 bytes
Finished in 0.015497684478759766s
Model has a coco model name, loading coco labels.

Building net ...
WARNING:tensorflow:From /content/darkflow/darkflow/net/build.py:105: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

Source | Train? | Layer description                | Output size
-------+--------+----------------------------------+---------------
WARNING:tensorflow:From /content/darkflow/darkflow/net/ops/baseop.py:70: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

WARNING:tensorflow:From /content/darkflow/darkflow/net/ops/baseop.py:71: The name tf.get_variable is deprecated. Please use tf.compat.v1.get_variable instead.

WARNING:tensorflow:From /content/darkflow/darkflow/net/ops/baseop.py:84: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.

       |   [...]<jupyter_text>Definimos las funciones:- `bounbox`: la cual nos permiten generar los recuadros que denotan la deteccion.- `intersect` y `ccw`: las cuales nos permiter detectar cuando una persona cruza un determinado umbral.<jupyter_code>def bounbox(imgcv, box):
  for box in result:
    if(box['label']=='person'):
      x1,y1,x2,y2 = box['topleft']['x'],box['topleft']['y'],box['bottomright']['x'],box['bottomright']['y']
      cx, cy = x1+int((x2-x1)/2), y1+int((y2-y1)/2)
      conf = box['confidence']
      label = box['label']
      cv2.rectangle(imgcv,(x1,y1),(x2,y2),(0,255,0),2)
      labelSize=cv2.getTextSize(label,cv2.FONT_HERSHEY_COMPLEX,0.5,1)
      confSize=cv2.getTextSize(label,cv2.FONT_HERSHEY_COMPLEX,0.5,1)
      _x1 = x1
      _y1 = y1#+int(labelSize[0][1]/2)
      _x2 = _x1+labelSize[0][0]
      _y2 = y1-int(labelSize[0][1])
      cv2.rectangle(imgcv,(_x1,_y1),(_x2,_y2),(0,255,0),cv2.FILLED)
      cv2.putText(imgcv,label,(x1,y1),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0),1)
      cv2.rectangle(imgcv,(_x1,_y1+14),(_x2,_y2+14),(0,255,0),cv2.FILLED)
      cv2.putText(imgcv,str(round(conf,2)),(x1,y1+14),cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,0),1)
      cv2.circle(imgcv,(cx,cy), 5, (0,0,255), -1)
  return imgcv

def intersect(A,B,C,D):
	return ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)
def ccw(A,B,C):
  return (C[1]-A[1]) * (B[0]-A[0])> (B[1]-A[1]) * (C[0]-A[0])
directory = "/content/darkflow/test_videos/"
output_directory = "/content/darkflow/outputs/"
ubication = 'back/'
amount    = 'uncrowd/'
paths = os.listdir(directory+ubication+amount)

line = [(0, 100), (320, 100)]
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(200, 3),	dtype="uint8")
for path in paths:
  print(path)
  mot_tracker = Sort()
  # capture = cv2.VideoCapture('2016_04_07_08_15_14BackColor.avi')
  capture = cv2.VideoCapture(directory+ubication+amount+path)

  ret, frame = capture.read()
  frame_height,frame_width,_ = frame.shape
  fourcc = cv2.VideoWriter_fourcc(*'XVID')
  out = cv2.VideoWriter(output_directory+ubication+amount+path,fourcc, 20, (frame_width,frame_height))

  mem = {}
  ret = True
  count = 0
  counter = 0
  previous = {}
  while(ret):
    ret, frame = capture.read()
    if(ret):
      # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
      result = tfnet.return_predict(frame)

      dets = []
      if len(result) > 0:
          # loop over the indexes we are keeping
          for res in result:
            if(res['label']=='person'):
              (x1,y1)=(res['topleft']['x'], res['topleft']['y'])
              (x2,y2)=(res['bottomright']['x'], res['bottomright']['y'])
              dets.append([x1, y1, x2, y2, res['confidence']])
      dets = np.asarray(dets)
      tracks = mot_tracker.update(dets)

      boxes = []
      indexIDs = []
      c = []
      previous = mem.copy()
      mem = {}
      for track in tracks:
        boxes.append([track[0], track[1], track[2], track[3]])
        indexIDs.append(int(track[4]))
        mem[indexIDs[-1]] = boxes[-1]

      if len(boxes) > 0:
        i = int(0)
        for box in boxes:
          # extract the bounding box coordinates
          (x, y) = (int(box[0]), int(box[1]))
          (w, h) = (int(box[2]), int(box[3]))

          color = [int(c) for c in COLORS[indexIDs[i] % len(COLORS)]]
          cv2.rectangle(frame, (x, y), (w, h), color, 2)
          # print(indexIDs)
          if indexIDs[i] in previous:
            previous_box = previous[indexIDs[i]]
            (x2, y2) = (int(previous_box[0]), int(previous_box[1]))
            (w2, h2) = (int(previous_box[2]), int(previous_box[3]))
            p0 = (int(x + (w-x)/2), int(y + (h-y)/2))
            p1 = (int(x2 + (w2-x2)/2), int(y2 + (h2-y2)/2))
            cv2.line(frame, p0, p1, color, 10)
            if intersect(p0, p1, line[0], line[1]):
              # print(i)
              # print(np.sum(np.square(np.array(p1)-np.array(p0))))
              # print(indexIDs[i])
              # if(np.sum(np.square(np.array(p1)-np.array(p0)))>25):
                counter += 1

          # text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
          # text = "{}".format(indexIDs[i])
          # cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
          i += 1

      # draw line
      cv2.line(frame, line[0], line[1], (0, 255, 255), 2)

      # draw counter
      cv2.putText(frame, str(counter), (300,20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 2)
      # counter += 1


      # if(len(result)>0):
        # frame = bounbox(frame,result)
      out.write(frame)
      # if(count%10==0):
      #   plt.imshow(frame)
      #   plt.show()
      count+=1

    # if(count>500):
      # ret = False

  capture.release()
  out.release()
for path in paths:
  print(path)
  mot_tracker = Sort()
  # capture = cv2.VideoCapture('2016_04_07_08_15_14BackColor.avi')
  capture = cv2.VideoCapture(directory+ubication+amount+path)

  ret, frame = capture.read()
  frame_height,frame_width,_ = frame.shape
  fourcc = cv2.VideoWriter_fourcc(*'XVID')
  out = cv2.VideoWriter(output_directory+ubication+amount+path,fourcc, 20, (frame_width,frame_height))

  mem = {}
  ret = True
  count = 0
  counter = 0
  disponible = 20
  previous = {}
  while(ret):
    ret, frame = capture.read()
    if(ret):
      # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
      result = tfnet.return_predict(frame)

      dets = []
      if len(result) > 0:
          # loop over the indexes we are keeping
          for res in result:
            if(res['label']=='person'):
              (x1,y1)=(res['topleft']['x'], res['topleft']['y'])
              (x2,y2)=(res['bottomright']['x'], res['bottomright']['y'])
              dets.append([x1, y1, x2, y2, res['confidence']])
      dets = np.asarray(dets)
      tracks = mot_tracker.update(dets)

      boxes = []
      indexIDs = []
      c = []
      previous = mem.copy()
      mem = {}
      for track in tracks:
        boxes.append([track[0], track[1], track[2], track[3]])
        indexIDs.append(int(track[4]))
        mem[indexIDs[-1]] = boxes[-1]

      if len(boxes) > 0:
        i = int(0)
        for box in boxes:
          # extract the bounding box coordinates
          (x, y) = (int(box[0]), int(box[1]))
          (w, h) = (int(box[2]), int(box[3]))

          color = [int(c) for c in COLORS[indexIDs[i] % len(COLORS)]]
          cv2.rectangle(frame, (x, y), (w, h), color, 2)
          # print(indexIDs)
          if indexIDs[i] in previous:
            previous_box = previous[indexIDs[i]]
            (x2, y2) = (int(previous_box[0]), int(previous_box[1]))
            (w2, h2) = (int(previous_box[2]), int(previous_box[3]))
            p0 = (int(x + (w-x)/2), int(y + (h-y)/2))
            p1 = (int(x2 + (w2-x2)/2), int(y2 + (h2-y2)/2))
            cv2.line(frame, p0, p1, color, 10)
            if intersect(p0, p1, line[0], line[1]):
              # print(i)
              # print(np.sum(np.square(np.array(p1)-np.array(p0))))
              # print(indexIDs[i])
              # if(np.sum(np.square(np.array(p1)-np.array(p0)))>25):
                counter += 1
                if ubication == 'back/':
                    disponible+= 1
                if ubication == 'front/':
                    disponible-= 1

          # text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
          # text = "{}".format(indexIDs[i])
          # cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
          i += 1

      # draw line
      cv2.line(frame, line[0], line[1], (0, 255, 0), 2)

      # draw counter
      if ubication == 'back/':
          cv2.putText(frame, "Salidas: "+str(counter), (210,200), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
          cv2.putText(frame, "Sitios disponibles: "+str(disponible), (150,225), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
      if ubication == 'front/':
          cv2.putText(frame, "Entradas: "+str(counter), (210,200), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
          cv2.putText(frame, "Sitios disponibles: "+str(disponible), (150,225), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
      # counter += 1
      if disponible < 0:
          cv2.putText(frame, "Sin Lugares disponibles", (120,120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)


      # if(len(result)>0):
        # frame = bounbox(frame,result)
      out.write(frame)
      # if(count%10==0):
        # plt.imshow(frame)
        # plt.show()
      count+=1

    # if(count>50):
      # ret = False

  capture.release()
  out.release()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1591197361745307, 'fraction_numerical': 0.05016489091831557, 'mean_word_length': 3.3998325892857144, 'pattern_counts': {'":': 3, '<': 25, '<?xml version=': 0, '>': 41, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '130918', 'license': 'Unlicense', 'n_tokens_mistral': 6355, 'n_tokens_neox': 5876, 'n_words': 1197, 'path': 'notebooks/yolo (1).ipynb', 'repo_name': 'IsRaTiAlv/Vision4Cities', 'size': '15768'}