summaryrefslogtreecommitdiff
path: root/autocam.py
blob: 405a6685432d014f65cda034cac393e9ee2a14c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import cv2
import numpy as np
import random
import time

print 'Press q to quit while the video is playing'

rec = raw_input('Record to video [y/n]? ').lower()[0] == 'y'

single = ['np.cos', 'np.sin', 'np.tan'] #Operations on a single number
binary = ['*', '+', '-'] #Operations for 2 numbers
numlist = ['frame', 't', 'Constant']

def randFunction(functionLength, singleweight, numberweight):
    hasframe = False
    hast = False
    while not(hasframe and hast):
        #Types: b for binary, s for single, f for first, n for number
        function = ''
        lasttype = 'f'
        thistype = 0
        hasframe = False
        hast = False
        chanceend = 0
        length = 1 #Number of operations done so far
        while True:
            chanceend = (1.0 - (1.0 / length)) ** (float(functionLength)/7)
            if lasttype == 'n':
                number = random.random()
                if number < chanceend:
                    break
                function = '(' + function + ')' + random.choice(binary)
                lasttype = 'b'
            elif lasttype == 's' or lasttype == 'b' or lasttype == 'f':
                function += '('
                thistype = random.random()
                if thistype < singleweight / (singleweight + numberweight):
                    function += random.choice(single)
                    lasttype = 's'
                else:
                    what = random.choice(numlist)
                    if what == 'Constant':
                        function += str(random.gauss(150, 50))
                    else:
                        function += what
                        if what == 'frame':
                            hasframe = True
                        elif what == 't':
                            hast = True
                    lasttype = 'n'
                    function += ')'
            length += 1
    if function.count('(') > function.count(')'):
        function += ')' * (function.count('(') - function.count(')'))
    return function

start = time.time()

t = 0
cap = cv2.VideoCapture(0)
function = randFunction(80, 1, 1)

try:
    w = cap.get(cv2.CV_CAP_PROP_FRAME_WIDTH)
    h = cap.get(cv2.CV_CAP_PROP_FRAME_HEIGHT)
except:
    w = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
    h = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)

if rec:
    try:
        fourcc = cv2.cv.CV_FOURCC(*'XVID')
    except:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(' '.join([namemaker3.generate() for i in range(3)]) + '.avi', fourcc, 10, (int(w),int(h)))


while True:
    t += 0.1
    ret, frame = cap.read()
    frame = frame.astype(float)
    frame = eval(function).astype(np.uint8)
    if rec:
        out.write(frame)
    cv2.imshow('Video',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    

cap.release()
if rec:
    out.release()
cv2.destroyAllWindows()