-
Notifications
You must be signed in to change notification settings - Fork 2
/
camrand.py
64 lines (50 loc) · 1.97 KB
/
camrand.py
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
import os, hashlib
from PIL import Image
from decimal import Decimal
import numpy as np
import subprocess
import pyximport; pyximport.install()
def hash_to_int(obj):
return int(hashlib.sha512(repr(obj).encode('utf-8')).hexdigest(), 16)
def numberToBase(n:int, b:int = -1, key:str or list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"):
if(b == -1): b = len(key)
digits = ""
while n:
digits += key[int(n % b)]
n //= b
return digits
class RandomImageSource:
def __init__(self):
self.last_random = 0
self.last_random = self.get_seed()
def color_algo(self, pxs:tuple):
return ''.join(map(lambda x: str(bin(x))[2:], pxs))
def gray_algo(self, pxs:tuple):
gray = int(sum(pxs) / len(pxs))
return str(bin(gray))[2:]
def bw_algo(self, pxs:tuple):
gray = sum(pxs) / len(pxs)
return "1" if gray > 127.5 else "0"
def get_raw_int(self, algorith=None):
if algorith == None: algorith = self.gray_algo
captureImage = subprocess.Popen(['fswebcam', '--no-banner', 'static.jpg'])
captureImage.communicate()
img = Image.open("./static.jpg").convert("RGB")
px = np.array(img).reshape(-1,3)
rand = ''.join(list(map(algorith, px)))
rand = int(rand, 2)
self.last_random = rand
return rand
def get_seed(self, threshold:int = 10000):
rand_int = self.last_random - self.get_raw_int(algorith=self.gray_algo)
if -threshold < rand_int < threshold: return self.last_random # do this to avoid returning 0
else: return rand_int if rand_int > 0 else -rand_int # only return positive numbers
def get_random(self):
seed = self.get_seed()
return Decimal(seed) / Decimal(int("1" * (len(str(bin(seed))) - 1), 2))
if __name__ == '__main__':
source = RandomImageSource()
print(source.get_raw_int())
seed = source.get_seed()
print(seed)
print(float(source.get_random()))