import numpy as np
import json
import math
import cv2
import sys


onlyLines = False
def processPoints(im, debug):

  # Convert to Grayscale
  imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  
  # Threshold the image with adaptative thresholding
  thresh = cv2.adaptiveThreshold(imgray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 2)

  # Find image contours
  (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

  # Output of "good" contours
  allcnts = []

  # Auxiliarry variables to help understand what is being processed (total points)
  totalP = 0

  # loop over our contours
  for c in cnts:
    # approximate the contour
    peri = cv2.arcLength(c, True)

    # If perimeter is too low, discart contour
    if peri < 100:
      continue

    # Try approximating poligon to simplify number of points
    approx = cv2.approxPolyDP(c, 6, True)

    # Sum total points processed
    totalP = totalP + len(approx)

    # Append contour to "good contours"
    allcnts.append(approx)

  # Debug total points
  print("Frame total points: " + str(totalP))

  # Debug: Show image contours
  if debug: 
    if onlyLines:
      out = im.copy() * 0
      cv2.drawContours(out, allcnts, -1, (0,255,0), 1)
    else:
      out = im

    # Debug: Show image
    cv2.imshow("thres", thresh)
    cv2.imshow("output", out)

  # Stored list of x y points in series
  points = []

  # Loop over selected contours and append everything to a single array of x, y, x, y, x, y... coordinates
  # points.append(1)
  # points.append(1)
  
  # points.append(1)
  # points.append(255)

  # points.append(255)
  # points.append(255)

  # points.append(255)
  # points.append(1)

  # points.append(1)
  # points.append(1)
  

  for contour in allcnts:
    for p in contour:
      x = p[0][0] * 1.0
      y = p[0][1] * 1.0
      xOut = int(math.floor(x / im.shape[0] * 255))
      yOut = int(math.floor(y / im.shape[1] * 255))
      points.append(xOut)
      points.append(yOut)
      points.append(0)

    points.append(0)
    points.append(0)
    points.append(0)

  # Return points
  return points

def savePoints(points, name):
  f = open('streams/' + name + '.json', 'w')
  f.write(json.dumps(points))
  f.close()

# im = cv2.imread('bat.png')

debug = True
fileName = 'beer.mp4'
if len(sys.argv) >= 2:
  fileName = sys.argv[2]

file = 'media/' + fileName
name = fileName.split('.')[0]

print()
print(' File input: ' + file)
print('  File Name: ' + name)
print('')

cap = cv2.VideoCapture(file)

# Stored list of frames
frames = []

# Flag indicating if it is recording or not
recording = True
while True: # cap.isOpened()):
    ret, frame = cap.read()

    if ret == False:
        cap.set(2 , 0)

        if not debug and recording:
          # Stop point saving
          recording = False

          # Output json data
          print('Saving ' + str(len(frames)) + ' frames')
          savePoints(frames, name)

          break;

        print("reseting frame")
        continue;

    points = processPoints(frame, debug)

    # Append frame to frames (will save everything later)
    if recording:
      frames.append(points)

    # Debug: Listen to inputs
    if debug:
      # Check for user keys input
      key = cv2.waitKey(debug) & 0xFF
      if key == ord('q'):
          break

      # Toggle showing lines or video
      if key == ord('t'):
        onlyLines = not onlyLines

# Release video and exit
cap.release()
cv2.destroyAllWindows()
222cbfe5045f4107b815ecad4a2318667b386a08
<mask token>


def processPoints(im, debug):
    imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    thresh = cv2.adaptiveThreshold(imgray, 255, cv2.
        ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 2)
    _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.
        CHAIN_APPROX_SIMPLE)
    allcnts = []
    totalP = 0
    for c in cnts:
        peri = cv2.arcLength(c, True)
        if peri < 100:
            continue
        approx = cv2.approxPolyDP(c, 6, True)
        totalP = totalP + len(approx)
        allcnts.append(approx)
    print('Frame total points: ' + str(totalP))
    if debug:
        if onlyLines:
            out = im.copy() * 0
            cv2.drawContours(out, allcnts, -1, (0, 255, 0), 1)
        else:
            out = im
        cv2.imshow('thres', thresh)
        cv2.imshow('output', out)
    points = []
    for contour in allcnts:
        for p in contour:
            x = p[0][0] * 1.0
            y = p[0][1] * 1.0
            xOut = int(math.floor(x / im.shape[0] * 255))
            yOut = int(math.floor(y / im.shape[1] * 255))
            points.append(xOut)
            points.append(yOut)
            points.append(0)
        points.append(0)
        points.append(0)
        points.append(0)
    return points


def savePoints(points, name):
    f = open('streams/' + name + '.json', 'w')
    f.write(json.dumps(points))
    f.close()


<mask token>

<mask token>


def processPoints(im, debug):
    imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    thresh = cv2.adaptiveThreshold(imgray, 255, cv2.
        ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 2)
    _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.
        CHAIN_APPROX_SIMPLE)
    allcnts = []
    totalP = 0
    for c in cnts:
        peri = cv2.arcLength(c, True)
        if peri < 100:
            continue
        approx = cv2.approxPolyDP(c, 6, True)
        totalP = totalP + len(approx)
        allcnts.append(approx)
    print('Frame total points: ' + str(totalP))
    if debug:
        if onlyLines:
            out = im.copy() * 0
            cv2.drawContours(out, allcnts, -1, (0, 255, 0), 1)
        else:
            out = im
        cv2.imshow('thres', thresh)
        cv2.imshow('output', out)
    points = []
    for contour in allcnts:
        for p in contour:
            x = p[0][0] * 1.0
            y = p[0][1] * 1.0
            xOut = int(math.floor(x / im.shape[0] * 255))
            yOut = int(math.floor(y / im.shape[1] * 255))
            points.append(xOut)
            points.append(yOut)
            points.append(0)
        points.append(0)
        points.append(0)
        points.append(0)
    return points


def savePoints(points, name):
    f = open('streams/' + name + '.json', 'w')
    f.write(json.dumps(points))
    f.close()


<mask token>
if len(sys.argv) >= 2:
    fileName = sys.argv[2]
<mask token>
print()
print(' File input: ' + file)
print('  File Name: ' + name)
print('')
<mask token>
while True:
    ret, frame = cap.read()
    if ret == False:
        cap.set(2, 0)
        if not debug and recording:
            recording = False
            print('Saving ' + str(len(frames)) + ' frames')
            savePoints(frames, name)
            break
        print('reseting frame')
        continue
    points = processPoints(frame, debug)
    if recording:
        frames.append(points)
    if debug:
        key = cv2.waitKey(debug) & 255
        if key == ord('q'):
            break
        if key == ord('t'):
            onlyLines = not onlyLines
cap.release()
cv2.destroyAllWindows()

<mask token>
onlyLines = False


def processPoints(im, debug):
    imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    thresh = cv2.adaptiveThreshold(imgray, 255, cv2.
        ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 2)
    _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.
        CHAIN_APPROX_SIMPLE)
    allcnts = []
    totalP = 0
    for c in cnts:
        peri = cv2.arcLength(c, True)
        if peri < 100:
            continue
        approx = cv2.approxPolyDP(c, 6, True)
        totalP = totalP + len(approx)
        allcnts.append(approx)
    print('Frame total points: ' + str(totalP))
    if debug:
        if onlyLines:
            out = im.copy() * 0
            cv2.drawContours(out, allcnts, -1, (0, 255, 0), 1)
        else:
            out = im
        cv2.imshow('thres', thresh)
        cv2.imshow('output', out)
    points = []
    for contour in allcnts:
        for p in contour:
            x = p[0][0] * 1.0
            y = p[0][1] * 1.0
            xOut = int(math.floor(x / im.shape[0] * 255))
            yOut = int(math.floor(y / im.shape[1] * 255))
            points.append(xOut)
            points.append(yOut)
            points.append(0)
        points.append(0)
        points.append(0)
        points.append(0)
    return points


def savePoints(points, name):
    f = open('streams/' + name + '.json', 'w')
    f.write(json.dumps(points))
    f.close()


debug = True
fileName = 'beer.mp4'
if len(sys.argv) >= 2:
    fileName = sys.argv[2]
file = 'media/' + fileName
name = fileName.split('.')[0]
print()
print(' File input: ' + file)
print('  File Name: ' + name)
print('')
cap = cv2.VideoCapture(file)
frames = []
recording = True
while True:
    ret, frame = cap.read()
    if ret == False:
        cap.set(2, 0)
        if not debug and recording:
            recording = False
            print('Saving ' + str(len(frames)) + ' frames')
            savePoints(frames, name)
            break
        print('reseting frame')
        continue
    points = processPoints(frame, debug)
    if recording:
        frames.append(points)
    if debug:
        key = cv2.waitKey(debug) & 255
        if key == ord('q'):
            break
        if key == ord('t'):
            onlyLines = not onlyLines
cap.release()
cv2.destroyAllWindows()

import numpy as np
import json
import math
import cv2
import sys
onlyLines = False


def processPoints(im, debug):
    imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    thresh = cv2.adaptiveThreshold(imgray, 255, cv2.
        ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 2)
    _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.
        CHAIN_APPROX_SIMPLE)
    allcnts = []
    totalP = 0
    for c in cnts:
        peri = cv2.arcLength(c, True)
        if peri < 100:
            continue
        approx = cv2.approxPolyDP(c, 6, True)
        totalP = totalP + len(approx)
        allcnts.append(approx)
    print('Frame total points: ' + str(totalP))
    if debug:
        if onlyLines:
            out = im.copy() * 0
            cv2.drawContours(out, allcnts, -1, (0, 255, 0), 1)
        else:
            out = im
        cv2.imshow('thres', thresh)
        cv2.imshow('output', out)
    points = []
    for contour in allcnts:
        for p in contour:
            x = p[0][0] * 1.0
            y = p[0][1] * 1.0
            xOut = int(math.floor(x / im.shape[0] * 255))
            yOut = int(math.floor(y / im.shape[1] * 255))
            points.append(xOut)
            points.append(yOut)
            points.append(0)
        points.append(0)
        points.append(0)
        points.append(0)
    return points


def savePoints(points, name):
    f = open('streams/' + name + '.json', 'w')
    f.write(json.dumps(points))
    f.close()


debug = True
fileName = 'beer.mp4'
if len(sys.argv) >= 2:
    fileName = sys.argv[2]
file = 'media/' + fileName
name = fileName.split('.')[0]
print()
print(' File input: ' + file)
print('  File Name: ' + name)
print('')
cap = cv2.VideoCapture(file)
frames = []
recording = True
while True:
    ret, frame = cap.read()
    if ret == False:
        cap.set(2, 0)
        if not debug and recording:
            recording = False
            print('Saving ' + str(len(frames)) + ' frames')
            savePoints(frames, name)
            break
        print('reseting frame')
        continue
    points = processPoints(frame, debug)
    if recording:
        frames.append(points)
    if debug:
        key = cv2.waitKey(debug) & 255
        if key == ord('q'):
            break
        if key == ord('t'):
            onlyLines = not onlyLines
cap.release()
cv2.destroyAllWindows()

import numpy as np
import json
import math
import cv2
import sys


onlyLines = False
def processPoints(im, debug):

  # Convert to Grayscale
  imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  
  # Threshold the image with adaptative thresholding
  thresh = cv2.adaptiveThreshold(imgray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 2)

  # Find image contours
  (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

  # Output of "good" contours
  allcnts = []

  # Auxiliarry variables to help understand what is being processed (total points)
  totalP = 0

  # loop over our contours
  for c in cnts:
    # approximate the contour
    peri = cv2.arcLength(c, True)

    # If perimeter is too low, discart contour
    if peri < 100:
      continue

    # Try approximating poligon to simplify number of points
    approx = cv2.approxPolyDP(c, 6, True)

    # Sum total points processed
    totalP = totalP + len(approx)

    # Append contour to "good contours"
    allcnts.append(approx)

  # Debug total points
  print("Frame total points: " + str(totalP))

  # Debug: Show image contours
  if debug: 
    if onlyLines:
      out = im.copy() * 0
      cv2.drawContours(out, allcnts, -1, (0,255,0), 1)
    else:
      out = im

    # Debug: Show image
    cv2.imshow("thres", thresh)
    cv2.imshow("output", out)

  # Stored list of x y points in series
  points = []

  # Loop over selected contours and append everything to a single array of x, y, x, y, x, y... coordinates
  # points.append(1)
  # points.append(1)
  
  # points.append(1)
  # points.append(255)

  # points.append(255)
  # points.append(255)

  # points.append(255)
  # points.append(1)

  # points.append(1)
  # points.append(1)
  

  for contour in allcnts:
    for p in contour:
      x = p[0][0] * 1.0
      y = p[0][1] * 1.0
      xOut = int(math.floor(x / im.shape[0] * 255))
      yOut = int(math.floor(y / im.shape[1] * 255))
      points.append(xOut)
      points.append(yOut)
      points.append(0)

    points.append(0)
    points.append(0)
    points.append(0)

  # Return points
  return points

def savePoints(points, name):
  f = open('streams/' + name + '.json', 'w')
  f.write(json.dumps(points))
  f.close()

# im = cv2.imread('bat.png')

debug = True
fileName = 'beer.mp4'
if len(sys.argv) >= 2:
  fileName = sys.argv[2]

file = 'media/' + fileName
name = fileName.split('.')[0]

print()
print(' File input: ' + file)
print('  File Name: ' + name)
print('')

cap = cv2.VideoCapture(file)

# Stored list of frames
frames = []

# Flag indicating if it is recording or not
recording = True
while True: # cap.isOpened()):
    ret, frame = cap.read()

    if ret == False:
        cap.set(2 , 0)

        if not debug and recording:
          # Stop point saving
          recording = False

          # Output json data
          print('Saving ' + str(len(frames)) + ' frames')
          savePoints(frames, name)

          break;

        print("reseting frame")
        continue;

    points = processPoints(frame, debug)

    # Append frame to frames (will save everything later)
    if recording:
      frames.append(points)

    # Debug: Listen to inputs
    if debug:
      # Check for user keys input
      key = cv2.waitKey(debug) & 0xFF
      if key == ord('q'):
          break

      # Toggle showing lines or video
      if key == ord('t'):
        onlyLines = not onlyLines

# Release video and exit
cap.release()
cv2.destroyAllWindows()