<commit_before>from sketchvision.ImageStrokeConverter import saveimg
from multiprocessing.queues import Queue
import multiprocessing
import commands
import cv
import numpy
import os
import threading
import time

######################################
# Image Manipulation
######################################
def getFillPoints(image):
    """Generate points from which iterative flood fill would cover all non-zero pixels
    in image."""
    image = cv.CloneMat(image)
    retList = []
    _, maxVal, _ , maxLoc = cv.MinMaxLoc(image)
    while maxVal > 0:
        retList.append(maxLoc)
        cv.FloodFill(image, maxLoc, 0)
        _, maxVal, _, maxLoc = cv.MinMaxLoc(image)
    return retList
        

def resizeImage(img, scale=None, dims=None):
    """Return a resized copy of the image for either relative
    scale, or that matches the dimensions given"""
    if scale is not None:
        retImg = cv.CreateMat(int(img.rows * scale), int(img.cols * scale), img.type)
    elif dims is not None:
        retImg = cv.CreateMat(dims[0], dims[1], img.type)
    else:
        retImg = cv.CloneMat(img)
    cv.Resize(img, retImg)
    return retImg


def warpFrame(frame, corners, targetCorners):
    """Transforms the frame such that the four corners (nw, ne, se, sw)
    match the targetCorners
    """
    outImg = cv.CreateMat(frame.rows, frame.cols, frame.type)
    if len(corners) == 4:
        #w,h = outImg.cols, outImg.rows #frame.cols, frame.rows
        warpMat = cv.CreateMat(3, 3, cv.CV_32FC1)  #Perspective warp matrix
        cv.GetPerspectiveTransform(corners,
            targetCorners,
            warpMat)
        #outImg = cv.CloneMat(frame)
        cv.WarpPerspective(frame, outImg, warpMat,
            (cv.CV_INTER_CUBIC | cv.CV_WARP_FILL_OUTLIERS), 255)
        return outImg
    else:
        return frame


def flipMat(image):
    """Flip an image vertically (top -> bottom)"""
    retImage = cv.CreateMat(image.rows, image.cols, image.type)
    height = image.rows
    transMatrix = cv.CreateMatHeader(2, 3, cv.CV_32FC1)
    narr = numpy.array([[1,0,0],[0,-1,height]], numpy.float32)
    cv.SetData(transMatrix, narr, cv.CV_AUTOSTEP)
    cv.WarpAffine(image, retImage, transMatrix)
    return retImage
    
    
def max_allChannel(image):
    """Return a grayscale image with values equal to the MAX
    over all 3 channels """
    retImage = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
    ch1 = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
    ch2 = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
    ch3 = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
    cv.Split(image, ch1, ch2, ch3, None)
    cv.Max(ch1, ch2, retImage)
    cv.Max(ch3, retImage, retImage)
    return retImage

    
def printMat(image):
    """Print out a text representation of an image"""
    for row in range(image.rows):
        print "[", 
        for col in range(image.cols):
            print cv.mGet(image, row, col), 
        print "]"
    print ""
        
        
######################################
# Video Capture Utils
######################################

def initializeCapture(cam = 0, dims=(1280, 1024,), disableAutoExposure = True, disableAutoFocus = True):
    """Try to initialize the capture to the requested dimensions,
    and disable auto-exposure. 
    Returns the capture and the actual dimensions"""
    capture = cv.CaptureFromCAM(cam)
    w, h = dims
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, h) 
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, w)
    reth = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
    retw = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
    if disableAutoExposure:
        setAutoExposure(cam, False)
    else:
        setAutoExposure(cam, True)
    if disableAutoFocus:
        setAutoFocus(cam, False)
    else:
        setAutoFocus(cam, True)
    return capture, (retw, reth,)

def setAutoExposure(cam, shouldAuto):
    if shouldAuto:
        value = 3
    else:
        value = 1
    os.system("v4l2-ctl -d {} --set-ctrl exposure_auto={}".format(cam,value))

def setAutoFocus(cam, shouldAuto):
    os.system("v4l2-ctl -d {} --set-ctrl focus_auto={}".format(cam,int(shouldAuto)))

def changeExposure(cam=0, increment=0):
    """Increase/Decrease the exposure of cam"""
    try:
        exposure = commands.getoutput("v4l2-ctl -d {} --get-ctrl exposure_absolute".format(cam)).split()[1]
        exposure = int(exposure)
        exposure = max(0, exposure+increment)
        commands.getoutput("v4l2-ctl -d {} --set-ctrl exposure_absolute={}".format(cam, exposure))
        print "Exposure {}".format(exposure)
    except Exception as e:
        print "Failed to change exposure: {}".format(e)


def captureImage(capture):
    """Capture a new image from capture, then set it as the data
    of gtkImage.
    Returns cv Image of the capture"""
    cvImg = cv.QueryFrame(capture)
    #cv.CvtColor(cvImg, cvImg, cv.CV_BGR2RGB)
    cvMat = cv.GetMat(cvImg)
    return cv.CloneMat(cvMat)

        
######################################
# Misc User Interaction Methods
######################################

def showResized(name, image, scale):
    """Combine resize and cv.ShowImage"""
    image = resizeImage(image, scale)<commit_msg>Can now set Exposure to an absolute value.<commit_after>from sketchvision.ImageStrokeConverter import saveimg
from multiprocessing.queues import Queue
import multiprocessing
import commands
import cv
import numpy
import os
import threading
import time

######################################
# Image Manipulation
######################################
def getFillPoints(image):
    """Generate points from which iterative flood fill would cover all non-zero pixels
    in image."""
    image = cv.CloneMat(image)
    retList = []
    _, maxVal, _ , maxLoc = cv.MinMaxLoc(image)
    while maxVal > 0:
        retList.append(maxLoc)
        cv.FloodFill(image, maxLoc, 0)
        _, maxVal, _, maxLoc = cv.MinMaxLoc(image)
    return retList
        

def resizeImage(img, scale=None, dims=None):
    """Return a resized copy of the image for either relative
    scale, or that matches the dimensions given"""
    if scale is not None:
        retImg = cv.CreateMat(int(img.rows * scale), int(img.cols * scale), img.type)
    elif dims is not None:
        retImg = cv.CreateMat(dims[0], dims[1], img.type)
    else:
        retImg = cv.CloneMat(img)
    cv.Resize(img, retImg)
    return retImg


def warpFrame(frame, corners, targetCorners):
    """Transforms the frame such that the four corners (nw, ne, se, sw)
    match the targetCorners
    """
    outImg = cv.CreateMat(frame.rows, frame.cols, frame.type)
    if len(corners) == 4:
        #w,h = outImg.cols, outImg.rows #frame.cols, frame.rows
        warpMat = cv.CreateMat(3, 3, cv.CV_32FC1)  #Perspective warp matrix
        cv.GetPerspectiveTransform(corners,
            targetCorners,
            warpMat)
        #outImg = cv.CloneMat(frame)
        cv.WarpPerspective(frame, outImg, warpMat,
            (cv.CV_INTER_CUBIC | cv.CV_WARP_FILL_OUTLIERS), 255)
        return outImg
    else:
        return frame


def flipMat(image):
    """Flip an image vertically (top -> bottom)"""
    retImage = cv.CreateMat(image.rows, image.cols, image.type)
    height = image.rows
    transMatrix = cv.CreateMatHeader(2, 3, cv.CV_32FC1)
    narr = numpy.array([[1,0,0],[0,-1,height]], numpy.float32)
    cv.SetData(transMatrix, narr, cv.CV_AUTOSTEP)
    cv.WarpAffine(image, retImage, transMatrix)
    return retImage
    
    
def max_allChannel(image):
    """Return a grayscale image with values equal to the MAX
    over all 3 channels """
    retImage = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
    ch1 = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
    ch2 = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
    ch3 = cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)
    cv.Split(image, ch1, ch2, ch3, None)
    cv.Max(ch1, ch2, retImage)
    cv.Max(ch3, retImage, retImage)
    return retImage

    
def printMat(image):
    """Print out a text representation of an image"""
    for row in range(image.rows):
        print "[", 
        for col in range(image.cols):
            print cv.mGet(image, row, col), 
        print "]"
    print ""
        
        
######################################
# Video Capture Utils
######################################

def initializeCapture(cam = 0, dims=(1280, 1024,), disableAutoExposure = True, disableAutoFocus = True):
    """Try to initialize the capture to the requested dimensions,
    and disable auto-exposure. 
    Returns the capture and the actual dimensions"""
    capture = cv.CaptureFromCAM(cam)
    w, h = dims
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, h) 
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, w)
    reth = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
    retw = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
    if disableAutoExposure:
        setAutoExposure(cam, False)
        changeExposure(cam, value=500)
    else:
        setAutoExposure(cam, True)
    if disableAutoFocus:
        setAutoFocus(cam, False)
    else:
        setAutoFocus(cam, True)
    return capture, (retw, reth,)

def setAutoExposure(cam, shouldAuto):
    if shouldAuto:
        value = 3
    else:
        value = 1
    os.system("v4l2-ctl -d {} --set-ctrl exposure_auto={}".format(cam,value))

def setAutoFocus(cam, shouldAuto):
    os.system("v4l2-ctl -d {} --set-ctrl focus_auto={}".format(cam,int(shouldAuto)))

def changeExposure(cam=0, increment=None, value=None):
    """Increase/Decrease the exposure of cam"""
    try:
        if increment is not None:
            exposure = commands.getoutput("v4l2-ctl -d {} --get-ctrl exposure_absolute".format(cam)).split()[1]
            exposure = int(exposure)
            exposure = max(0, exposure+increment)
        elif value is not None:
            exposure = max(0, value)
        else:
            raise Exception("increment or value must be an integer")
        commands.getoutput("v4l2-ctl -d {} --set-ctrl exposure_absolute={}".format(cam, exposure))
        print "Exposure {}".format(exposure)
    except Exception as e:
        print "Failed to change exposure: {}".format(e)


def captureImage(capture):
    """Capture a new image from capture, then set it as the data
    of gtkImage.
    Returns cv Image of the capture"""
    cvImg = cv.QueryFrame(capture)
    #cv.CvtColor(cvImg, cvImg, cv.CV_BGR2RGB)
    cvMat = cv.GetMat(cvImg)
    return cv.CloneMat(cvMat)

        
######################################
# Misc User Interaction Methods
######################################

def showResized(name, image, scale):
    """Combine resize and cv.ShowImage"""
    image = resizeImage(image, scale)
{'fraction_non_alphanumeric': 0.15034546292031323, 'fraction_numerical': 0.012620912022109626, 'mean_word_length': 3.212650368645712, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1650095', 'n_tokens_mistral': 3559, 'n_tokens_neox': 3410, 'n_words': 889}