<commit_before>#!/usr/bin/python
#
#
# imgExplore.py
#   a python/opencv utility for perusing video or image streams.
#   Performance issues can be resolved by migrating algorithms to
#   gpu. (VisionWorks, Cuda, etc are available optimization avenues)
#
#   db: 1/28/16, for spartronics 4915
#
#
from __future__ import print_function
import cv2
import os.path
import sys, traceback
import argparse
import numpy as np
import math
import daemon
from multiprocessing.pool import ThreadPool
from collections import deque

import robotCnx
import planeTracker
import common

class App:
    def __init__(self):
        self.args = self.parseArgs()
        if self.args.daemonize:
            with daemon.DaemonContext():
                self.go()
        else:
            self.go()

    def go(self):
        # args is argparser.Namespace: an object whose members are 
        # accessed as:  self.args.cannedimages

        self.robotCnx = robotCnx.RobotCnx(self.args.fakerobot)

        self.vsrc = None
        if self.args.cannedimages:
            self.fnpat = '../../pictures/RealFullField/%d.jpg'
        else:
            self.fnpat = None
        self.index = 0
        self.lastFn = None
        self.mainImg = None
        self.algostate = {}  # stores persistant objects depending on cmode
        self.threadn = cv2.getNumberOfCPUs()
        self.pool = ThreadPool(processes = self.threadn)
        self.pending = deque()
        self.threadedMode = True
        self.update = False
        self.latency = common.StatValue()   
        self.frameT = common.StatValue() 
        self.lastFrameTime = common.clock()
        self.lastStashTime = 0
        self.stashFilename = "/var/tmp/imgServer.home/currentImage.jpg"
        self.stashParams = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
        self.zeros = (0,0,0,0,0,0)

        self.indent = ' ' * 50
        self.lastStatus = ""

        # cmodelist maps a number [0, len] to an cmode/algorithm
        self.cmodelist = [
            'rgb',
            'adaptiveThreshold',
            'threshold',
            'huerange*valrange',
            'canny',
            'simpleblob',
            'houghlines',
            'contours',
            'ORB',

            'b',
            'g',
            'h',
            'r',
            's',
            'v',
        ]

        # cmodeValueCache stores the edited values associated with each cmode.
        # It starts empty, then builds up state as cmode changes.
        self.cmodeValueCache = {
            'adaptiveThreshold': [-4, 0, 0, 0, 0, 0],
            'threshold':         [75, 0, 0, 0, 0, 0],
            'huerange*valrange': [79, 91, 100, 255, 0, 0],
            'canny':             [10, 200, 0, 0, 0, 0],
            'simpleblob':        [75, 150, 5, 150, 0, 0], 
                          # minThresh
                          # maxThresh
                          # thresStep
                          # minSize (maxsize is 'large')
            'houghlines':        [2, 5, 10, 30, 2, 0], 
                          # rho is distance resolution in pixels
                          # theta is angle in degress (larger -> more lines)
                          # threshold is measured in 'votes', higher -> fewer 
                          # minLineLength
                          # maxLineGap
            'contours':          [1, 0, 0, -1, 1, 0],
                          # mode: [0-3]
                          # method: [CHAIN_APPROX_NONE,_SIMPLE,_TC89_L1, KOS]
                          # offset shift for contour
                          # unused
                          # which contour to draw, -1 is all
                          # which level of the contours to draw
            'ORB':              [40, 10, 8, 31, 0, 0],
                          # nfeatures
                          # scaleFactor [1 -> 2  (0->255)]
                          # nlevels
                          # patchSizea == edgeThreshold
            'r': self.zeros,
            'g': self.zeros,
            'b': self.zeros,
            'h': self.zeros,
            's': self.zeros,
            'v': self.zeros,
            'rgb': self.zeros
        }

        #keyToCmode maps a key to a cmod
        self.keyToCmode = {
            49: self.cmodelist[1], # adaptiveThreshold on '1' key
            50: self.cmodelist[2],
            51: self.cmodelist[3],
            52: self.cmodelist[4],
            53: self.cmodelist[5],
            54: self.cmodelist[6], # houghlines
            55: self.cmodelist[7], # contours
            56: self.cmodelist[8], # ORB

            98: 'b',
            99: 'rgb',
            103: 'g',
            104: 'h',
            114: 'r',
            115: 's',
            118: 'v',
        }

        self.keyToFrameChange = {
            81: 'b',
            83: 'n',
        }

        self.algoValueChange = {
            127: ("reset",  self.zeros),  # keypad clear

            190: ("v1Down",  (-1, 0, 0, 0, 0, 0)), # f1
            191: ("v1Up  ",  (1, 0, 0, 0, 0, 0)),  # f2

            84: ("v1Down",  (-1, 0, 0, 0, 0, 0)), # downarrow
            82: ("v1Up  ",  (1, 0, 0, 0, 0, 0)),  # uparrow

            191: ("v1Up  ",  (1, 0, 0, 0, 0, 0)),  # downarrow

            192: ("v2Down",  (0, -1, 0, 0, 0, 0)), # f3
            193: ("v2Up  ",  (0, 1, 0, 0, 0, 0)),  # f4

            194: ("v3Down",  (0, 0, -1, 0, 0, 0)), # f5
            195: ("v3Up  ",  (0, 0, 1, 0, 0, 0)),  # f6

            196: ("v4Down",  (0, 0, 0, -1, 0, 0)), # f7
            197: ("v4Up  ",  (0, 0, 0, 1, 0, 0)), # f8

            198: ("v5Down",  (0, 0, 0, 0, -1, 0)), # f9
            199: ("v5Up  ",  (0, 0, 0, 0, 1, 0)), # f10

            200: ("v6Down",  (0, 0, 0, 0, 0, -1)), # f10
            201: ("v6Up  ",  (0, 0, 0, 0, 0, 1)),  # f11
        }

        self.cmode = self.getCmode(self.args.algorithm)
        self.values = self.getCmodeValues(self.cmode)

    # end of __init__

    # -----------------------------------------------------------------
    def Run(self):
        if self.args.nodisplay:
            print("\nimgExplore: running in nodisplay mode (keys inactive)")
        else:
            print("\n\nkeys:\n" \
      "  ESC: exit\n\n" \
      "  c: rgb\n" \
      "  r: red\n" \
      "  g: green\n" \
      "  b: blue,\n" \
      "  h: hue\n" \
      "  s: sat\n"\
      "  v: val\n" \
      "  1: adaptive threshold (thresh,inv)\n"\
      "  2: threshold          (thresh,inv)\n"\
      "  3: huerange*valrange  (hmin,hmax,vmin,vmax)\n"\
      "  4: canny edges        (thresh/10, range)\n"\
      "  5: simple blobs       (thresh0,thresh1,minA,aMult,colorthresh)\n"\
      "  6: houghlines         (rho,theta,thresh,minlen,maxgap)\n"\
      "  7: contours           (mode:0-3,method:0-3,offset,id(-1:all),depth)\n"\
      "  8: ORB features       (nfeatures,scaleFactor(0->255)],patchSize)\n"\
      "  9: PlaneTracker       (unimplemented)\n"\
      "\n"\
      "  F1,F2:            -/+ v1\n"\
      "  F3,F4:            -/+ v2\n"\
      "  F5,F6:            -/+ v3\n"\
      "  F7,F8:            -/+ v4\n"\
      "  F9,F10:           -/+ v5\n"\
      "  F10,F11:          -/+ v6\n"\
      "\n"\
      "  <right arrow>:    increase img seq frame\n"\
      "  <left arrow>:     decrease img seq frame\n"\
          )

        if not self.fnpat:
            vsrc = cv2.VideoCapture(0)
            if not vsrc or not vsrc.isOpened():
                print("Problem opening video source")
                vsrc = None
                exit(1)
            else:
                ret1 = vsrc.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
                ret2 = vsrc.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 720)
                print(ret1, ret2)
                if 1:
                    w = vsrc.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
                    h = vsrc.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
                    print("video res: %d %d" % (w,h))
        else:
            vsrc = None

        self.update = False
        while True:
            while len(self.pending) > 0 and self.pending[0].ready():
                frame,t0,keypoints,lines,contours = self.pending.popleft().get()<commit_msg>Initial support for more than one video source.<commit_after>#!/usr/bin/python
#
#
# imgExplore.py
#   a python/opencv utility for perusing video or image streams.
#   Performance issues can be resolved by migrating algorithms to
#   gpu. (VisionWorks, Cuda, etc are available optimization avenues)
#
#   db: 1/28/16, for spartronics 4915
#
#
from __future__ import print_function
import cv2
import os.path
import sys, traceback
import argparse
import numpy as np
import math
import daemon
from multiprocessing.pool import ThreadPool
from collections import deque

import robotCnx
import planeTracker
import common

class App:
    def __init__(self):
        self.args = self.parseArgs()
        if self.args.daemonize:
            with daemon.DaemonContext():
                self.go()
        else:
            self.go()

    def go(self):
        # args is argparser.Namespace: an object whose members are 
        # accessed as:  self.args.cannedimages

        self.robotCnx = robotCnx.RobotCnx(self.args.fakerobot)

        self.vsrc = None
        if self.args.cannedimages:
            self.fnpat = '../../pictures/RealFullField/%d.jpg'
        else:
            self.fnpat = None
        self.index = 0
        self.lastFn = None
        self.mainImg = None
        self.algostate = {}  # stores persistant objects depending on cmode
        self.threadn = cv2.getNumberOfCPUs()
        self.pool = ThreadPool(processes = self.threadn)
        self.pending = deque()
        self.threadedMode = True
        self.update = False
        self.latency = common.StatValue()   
        self.frameT = common.StatValue() 
        self.lastFrameTime = common.clock()
        self.lastStashTime = 0
        self.stashFilename = "/var/tmp/imgServer.home/currentImage.jpg"
        self.stashParams = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
        self.zeros = (0,0,0,0,0,0)

        self.indent = ' ' * 50
        self.lastStatus = ""

        # cmodelist maps a number [0, len] to an cmode/algorithm
        self.cmodelist = [
            'rgb',
            'adaptiveThreshold',
            'threshold',
            'huerange*valrange',
            'canny',
            'simpleblob',
            'houghlines',
            'contours',
            'ORB',

            'b',
            'g',
            'h',
            'r',
            's',
            'v',
        ]

        # cmodeValueCache stores the edited values associated with each cmode.
        # It starts empty, then builds up state as cmode changes.
        self.cmodeValueCache = {
            'adaptiveThreshold': [-4, 0, 0, 0, 0, 0],
            'threshold':         [75, 0, 0, 0, 0, 0],
            'huerange*valrange': [79, 91, 100, 255, 0, 0],
            'canny':             [10, 200, 0, 0, 0, 0],
            'simpleblob':        [75, 150, 5, 150, 0, 0], 
                          # minThresh
                          # maxThresh
                          # thresStep
                          # minSize (maxsize is 'large')
            'houghlines':        [2, 5, 10, 30, 2, 0], 
                          # rho is distance resolution in pixels
                          # theta is angle in degress (larger -> more lines)
                          # threshold is measured in 'votes', higher -> fewer 
                          # minLineLength
                          # maxLineGap
            'contours':          [1, 0, 0, -1, 1, 0],
                          # mode: [0-3]
                          # method: [CHAIN_APPROX_NONE,_SIMPLE,_TC89_L1, KOS]
                          # offset shift for contour
                          # unused
                          # which contour to draw, -1 is all
                          # which level of the contours to draw
            'ORB':              [40, 10, 8, 31, 0, 0],
                          # nfeatures
                          # scaleFactor [1 -> 2  (0->255)]
                          # nlevels
                          # patchSizea == edgeThreshold
            'r': self.zeros,
            'g': self.zeros,
            'b': self.zeros,
            'h': self.zeros,
            's': self.zeros,
            'v': self.zeros,
            'rgb': self.zeros
        }

        #keyToCmode maps a key to a cmod
        self.keyToCmode = {
            49: self.cmodelist[1], # adaptiveThreshold on '1' key
            50: self.cmodelist[2],
            51: self.cmodelist[3],
            52: self.cmodelist[4],
            53: self.cmodelist[5],
            54: self.cmodelist[6], # houghlines
            55: self.cmodelist[7], # contours
            56: self.cmodelist[8], # ORB

            98: 'b',
            99: 'rgb',
            103: 'g',
            104: 'h',
            114: 'r',
            115: 's',
            118: 'v',
        }

        self.keyToFrameChange = {
            81: 'b',
            83: 'n',
        }

        self.algoValueChange = {
            127: ("reset",  self.zeros),  # keypad clear

            190: ("v1Down",  (-1, 0, 0, 0, 0, 0)), # f1
            191: ("v1Up  ",  (1, 0, 0, 0, 0, 0)),  # f2

            84: ("v1Down",  (-1, 0, 0, 0, 0, 0)), # downarrow
            82: ("v1Up  ",  (1, 0, 0, 0, 0, 0)),  # uparrow

            191: ("v1Up  ",  (1, 0, 0, 0, 0, 0)),  # downarrow

            192: ("v2Down",  (0, -1, 0, 0, 0, 0)), # f3
            193: ("v2Up  ",  (0, 1, 0, 0, 0, 0)),  # f4

            194: ("v3Down",  (0, 0, -1, 0, 0, 0)), # f5
            195: ("v3Up  ",  (0, 0, 1, 0, 0, 0)),  # f6

            196: ("v4Down",  (0, 0, 0, -1, 0, 0)), # f7
            197: ("v4Up  ",  (0, 0, 0, 1, 0, 0)), # f8

            198: ("v5Down",  (0, 0, 0, 0, -1, 0)), # f9
            199: ("v5Up  ",  (0, 0, 0, 0, 1, 0)), # f10

            200: ("v6Down",  (0, 0, 0, 0, 0, -1)), # f10
            201: ("v6Up  ",  (0, 0, 0, 0, 0, 1)),  # f11
        }

        self.cmode = self.getCmode(self.args.algorithm)
        self.values = self.getCmodeValues(self.cmode)

    # end of __init__

    # -----------------------------------------------------------------
    def Run(self):
        if self.args.nodisplay:
            print("\nimgExplore: running in nodisplay mode (keys inactive)")
        else:
            print("\n\nkeys:\n" \
      "  ESC: exit\n\n" \
      "  c: rgb\n" \
      "  r: red\n" \
      "  g: green\n" \
      "  b: blue,\n" \
      "  h: hue\n" \
      "  s: sat\n"\
      "  v: val\n" \
      "  1: adaptive threshold (thresh,inv)\n"\
      "  2: threshold          (thresh,inv)\n"\
      "  3: huerange*valrange  (hmin,hmax,vmin,vmax)\n"\
      "  4: canny edges        (thresh/10, range)\n"\
      "  5: simple blobs       (thresh0,thresh1,minA,aMult,colorthresh)\n"\
      "  6: houghlines         (rho,theta,thresh,minlen,maxgap)\n"\
      "  7: contours           (mode:0-3,method:0-3,offset,id(-1:all),depth)\n"\
      "  8: ORB features       (nfeatures,scaleFactor(0->255)],patchSize)\n"\
      "  9: PlaneTracker       (unimplemented)\n"\
      "\n"\
      "  F1,F2:            -/+ v1\n"\
      "  F3,F4:            -/+ v2\n"\
      "  F5,F6:            -/+ v3\n"\
      "  F7,F8:            -/+ v4\n"\
      "  F9,F10:           -/+ v5\n"\
      "  F10,F11:          -/+ v6\n"\
      "\n"\
      "  <right arrow>:    increase img seq frame\n"\
      "  <left arrow>:     decrease img seq frame\n"\
          )

        if not self.fnpat:
            for i in range(0, 4):
                vsrc = cv2.VideoCapture(i)
                if not vsrc or not vsrc.isOpened():
                    print("Problem opening video source %d" % i)
                    vsrc = None
                else:
                    break

            if not vsrc:
                exit(1)
            else:
                ret1 = vsrc.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
                ret2 = vsrc.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 720)
                print(ret1, ret2)
                if 1:
                    w = vsrc.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
                    h = vsrc.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
                    print("video res: %d %d" % (w,h))
        else:
            vsrc = None

        self.update = False
        while True:
            while len(self.pending) > 0 and self.pending[0].ready():
                frame,t0,keypoints,lines,contours = self.pending.popleft().get()
{'fraction_non_alphanumeric': 0.14212734902390076, 'fraction_numerical': 0.046767621480265156, 'mean_word_length': 1.2791406791406792, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4743639', 'n_tokens_mistral': 5950, 'n_tokens_neox': 5369, 'n_words': 1340}