<commit_before>"""Single process image processing."""

import datetime
import sys
import cv2
import numpy as np

import util
import iproc

DEVICE   = int(sys.argv[1])
WIDTH    = int(sys.argv[2])
HEIGHT   = int(sys.argv[3])
DURATION = float(sys.argv[4])  # In seconds.

# Create the OpenCV video capture object.
cap = cv2.VideoCapture(DEVICE)
cap.set(3, WIDTH)
cap.set(4, HEIGHT)

# Create the output windows.
NAMES = (
    'image',
#    'image_pre', 
#    'image_diff', 
#    'image_thresh',
#    'image_acc',
    )
for name in NAMES:
    cv2.namedWindow(name, cv2.cv.CV_WINDOW_NORMAL)

# Accumulation of thresholded differences.
image_acc = None  

def step1(image):
    """Return preprocessed image."""
    return iproc.preprocess2(image)
 
def step2(image, alpha):
    """Compute difference between given image and accumulation,
    then accumulate and return the difference. Initialize accumulation
    if needed (if opacity is 100%.)"""

    # Use global accumulation.
    global image_acc

    # Initalize accumulation if so indicated.
    if alpha == 1.0:
        image_acc = np.empty(np.shape(image))

    # Compute difference.
    image_diff = cv2.absdiff(
        image_acc.astype(image.dtype),
        image,
        )

    # Accumulate.
    hello = cv2.accumulateWeighted(
        image,
        image_acc,
        alpha,
        )

    return image_diff

# Monitor framerates for the given seconds past.
framerate = util.RateTicker((1,5,10))

def step3(image, image_diff):
    """Postprocess image using given difference."""
    iproc.postprocess(image, image_diff)

    # Write the framerate on top of the image.
    iproc.writeOSD(
        image, 
        ('%.2f, %.2f, %.2f fps'%framerate.tick(),),
        ratio=0.04,
        )
    
def step4():
    """Display the result of processing."""
    # Show the images.
    for name in NAMES:
        exec('the_image = %s'%name)
        cv2.imshow(name, the_image)
    cv2.waitKey(1)

# Keep track of previous iteration's timestamp.
tstamp_prev = None  

end = datetime.datetime.now() + datetime.timedelta(seconds=DURATION)
while end > datetime.datetime.now():

    # Take a snapshot and mark the snapshot time.
    hello, image = cap.read()

    # Compute alpha value.
    alpha, tstamp_prev = iproc.getAlpha(tstamp_prev)

    # Preprocess the image.
    image_pre = step1(image)
    
    # Compute difference, and accumulate.
    image_diff = step2(image_pre, alpha)

    # Augment.
    step3(image, image_diff)

    # Display.
    step4()

# The end.
<commit_msg>Unroll functionalized steps inline into main loop.<commit_after>"""Simple single-process sequential image processing."""

import datetime
import sys
import cv2
import numpy as np

import util
import iproc

DEVICE   = int(sys.argv[1])
WIDTH    = int(sys.argv[2])
HEIGHT   = int(sys.argv[3])
DURATION = float(sys.argv[4])  # In seconds.

# Create the OpenCV video capture object.
cap = cv2.VideoCapture(DEVICE)
cap.set(3, WIDTH)
cap.set(4, HEIGHT)

# Create the output window.
cv2.namedWindow('motion 1', cv2.cv.CV_WINDOW_NORMAL)

# Maintain accumulation of thresholded differences.
image_acc = None  

# Keep track of previous iteration's timestamp.
tstamp_prev = None  

# Monitor framerates for the given seconds past.
framerate = util.RateTicker((1,5,10))

# Run the loop for designated amount of time.
end = datetime.datetime.now() + datetime.timedelta(seconds=DURATION)
while end > datetime.datetime.now():

    # Take a snapshot and mark the snapshot time.
    hello, image = cap.read()

    # Compute alpha value.
    alpha, tstamp_prev = iproc.getAlpha(tstamp_prev)

    # Preprocess the image.
    image_pre = iproc.preprocess2(image)

    # Initalize accumulation if so indicated.
    if image_acc is None:
        image_acc = np.empty(np.shape(image_pre))

    # Compute difference.
    image_diff = cv2.absdiff(
        image_acc.astype(image_pre.dtype),
        image_pre,
        )

    # Accumulate.
    hello = cv2.accumulateWeighted(
        image_pre,
        image_acc,
        alpha,
        )

    # Draw the difference on top of the image.
    iproc.postprocess(image, image_diff)

    # Write the framerate on top of the image.
    iproc.writeOSD(
        image, 
        ('%.2f, %.2f, %.2f fps'%framerate.tick(),),
        ratio=0.04,
        )

    # Display the image.
    cv2.imshow('motion 1', image)

    # Allow HighGUI to process event.
    cv2.waitKey(1)

# The end.

{'fraction_non_alphanumeric': 0.1087251356238698, 'fraction_numerical': 0.015144665461121157, 'mean_word_length': 2.898678414096916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1584793', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1442, 'n_words': 401}