aboutsummaryrefslogtreecommitdiffstats
path: root/entropy_harvester
diff options
context:
space:
mode:
authorDavid Runge <david.runge@frqrec.com>2014-02-15 01:32:28 +0100
committerDavid Runge <david.runge@frqrec.com>2014-02-15 01:32:28 +0100
commit29d861e10548e25339e24cfd2db1e3a9fc748e41 (patch)
treee55f4104dbd07ef285a325b1e50a61628522b50b /entropy_harvester
parent7aa2d1e204c96c7c61210361f0469c23ff11450b (diff)
downloadrandom241-29d861e10548e25339e24cfd2db1e3a9fc748e41.tar.gz
random241-29d861e10548e25339e24cfd2db1e3a9fc748e41.tar.bz2
random241-29d861e10548e25339e24cfd2db1e3a9fc748e41.tar.xz
random241-29d861e10548e25339e24cfd2db1e3a9fc748e41.zip
Adding random241*.py files to subdirectory
Diffstat (limited to 'entropy_harvester')
-rwxr-xr-xentropy_harvester/random241.py68
-rwxr-xr-xentropy_harvester/random241arg.py25
-rwxr-xr-xentropy_harvester/random241osc.py32
-rwxr-xr-xentropy_harvester/random241sensor.py186
4 files changed, 311 insertions, 0 deletions
diff --git a/entropy_harvester/random241.py b/entropy_harvester/random241.py
new file mode 100755
index 0000000..f5268d5
--- /dev/null
+++ b/entropy_harvester/random241.py
@@ -0,0 +1,68 @@
+#!/usr/bin/python2
+
+import cv
+import time
+import numpy as np
+import logging
+import random241arg as arg
+import random241sensor as sensor
+import random241osc as osc
+
+showStream = False
+stop_key = 0
+capture = True
+camNumber = 1
+time_delta = 60 * 60 / 2
+logging.basicConfig(filename='random241.log',
+ format='%(asctime)s %(message)s',
+ filemode='w', level=logging.INFO)
+last_time = time.time()
+
+# Read parameters
+params = arg.read_params()
+# Define which cam to use, etc.
+#cam = sensor.capture(params['-c'], showStream)
+
+# Get frame and size information from camera
+cam = cv.CaptureFromCAM(camNumber)
+frame = cv.QueryFrame(cam)
+if frame is not None:
+ frame_size = cv.GetSize(frame)
+ logging.info('Grabbing random numbers from: %dx%dpx.',
+ frame_size[0], frame_size[1])
+
+ # Get matrix with values from frame
+ mat = cv.GetMat(frame)
+ frame_values = np.asarray(mat)
+ # Create grayscale image
+ gray_values = sensor.bgr2gray(frame_values)
+
+ # Define the time to run the test
+ time_delta = time_delta + time.time()
+
+ # Setup OSC
+ #osc.connect_to_server('beagleclone', 57120)
+ osc.connect_to_server('127.0.0.1', 57120)
+
+ # Main loop for accessing the camera and calculating random numbers from it
+ #while True:
+ while time_delta > time.time():
+# last_time = time.time()
+ img = cv.QueryFrame(cam)
+ # Get a numpy array with rgb values
+ mat_from_frame = sensor.frame_to_mat(img)
+ gray_mat = sensor.bgr2gray(mat_from_frame)
+ randomness = sensor.harvest_entropy(gray_mat)
+ if randomness is not None:
+ delta = time.time() - last_time
+ last_time = time.time()
+ osc.send_msg(delta, randomness)
+ if showStream:
+ while stop_key != ord("q"):
+ cv.ShowImage("Americium 241", img)
+ key = cv.WaitKey(2)
+
+ #time.sleep(5)
+ #random241sensor.set_capture(False)
+else:
+ logging.error('Connect camera %d first!', camNumber)
diff --git a/entropy_harvester/random241arg.py b/entropy_harvester/random241arg.py
new file mode 100755
index 0000000..4bd00d8
--- /dev/null
+++ b/entropy_harvester/random241arg.py
@@ -0,0 +1,25 @@
+#!/usr/bin/python2
+
+from sys import argv
+
+
+def read_params():
+ # Checks the given parameters
+ info = """Use the program as following:
+ random241.py [option] <param> ...
+ Options:
+ -c number of cam to use (starts with first found camera 0 (default))
+ -r remote address to send the output to (standard 192.168.0.7)
+ """
+ default = {'-c': "1", '-r': "192.168.0.7"}
+ parameters = default.copy()
+ if len(argv) != 3:
+ for i in xrange(1, len(argv) - 1, 2):
+ if argv[i] in parameters:
+ parameters[argv[i]] = argv[i + 1]
+ else:
+ print info
+ return 0
+ return parameters
+ else:
+ return default
diff --git a/entropy_harvester/random241osc.py b/entropy_harvester/random241osc.py
new file mode 100755
index 0000000..1e06917
--- /dev/null
+++ b/entropy_harvester/random241osc.py
@@ -0,0 +1,32 @@
+#import OSC as osc
+import liblo as osc
+import logging
+
+# Declare an empty target
+target = None
+
+
+# Connect to the server
+def connect_to_server(hostname, port):
+ global target
+ if (hostname or port) is None:
+ target = osc.Adress('127.0.0.1', 57121, osc.UDP)
+ else:
+ try:
+ target = osc.Address(hostname, port, osc.UDP)
+ except osc.AddressError, err:
+ logging.error(err)
+
+
+# Send a osc_message to the server
+def send_msg(time_delta, randomness):
+ global target
+ # if the message is not empty and longer than 1
+ if randomness is not None and len(randomness) > 1:
+ msg = osc.Message("/random")
+ msg.add(time_delta)
+ msg.add(randomness[0], randomness[1])
+ try:
+ osc.send(target, msg)
+ except:
+ logging.error('OSC: Sending of message failed.')
diff --git a/entropy_harvester/random241sensor.py b/entropy_harvester/random241sensor.py
new file mode 100755
index 0000000..93b7276
--- /dev/null
+++ b/entropy_harvester/random241sensor.py
@@ -0,0 +1,186 @@
+#!/usr/bin/python2
+
+import logging
+import cv
+import numpy as np
+import time
+
+# Bool to define wether to capture the cam or not
+capture = True
+# Bool to define wether to show the capture stream or not
+showStream = True
+white_threshold = 17.0
+checked = np.zeros((1, 1), dtype=np.int)
+mat = np.zeros((1, 1))
+clusters = []
+balances = []
+
+
+def capture(camNumber, showStream):
+ # Open stream for that camera
+ logging.info('Capture from camera #%d', camNumber)
+ cam = cv.CaptureFromCAM(int(camNumber))
+ # Stream to output window as long as it is active
+ return cam
+ while capture:
+ stream = cv.QueryFrame(cam)
+ if showStream:
+ cv.ShowImage("Americium 241", stream)
+
+
+def set_capture(onOrOff):
+ if onOrOff == bool:
+ global capture
+ capture = onOrOff
+
+
+def frame_to_mat(img):
+ cv.Smooth(img, img, cv.CV_GAUSSIAN, 3, 0)
+ mat = cv.GetMat(img)
+ frame_values = np.asarray(mat)
+ return frame_values
+
+
+# Convert a bgr matrix to grayscale
+def bgr2gray(mat):
+ b, g, r = mat[:, :, 0], mat[:, :, 1], mat[:, :, 2]
+ gray = 0.1140 * b + 0.5870 * g + 0.2989 * r
+ return gray
+
+
+# Find a white dot in the black input matrix
+def harvest_entropy(mat_input):
+ global mat
+ global checked
+ global clusters
+ global balances
+ mat = mat_input.copy()
+ if np.ndim(mat) >= 2:
+ # Create array to hold the already checked pixels
+ checked = np.zeros((len(mat), len(mat[0])), dtype=np.int)
+ # Traverse the grayscale values in search of a bright pixel
+ for i in range(0, len(mat) - 1):
+ for j in range(0, len(mat[0]) - 1):
+ # Check if it hasn't been checked yet
+ if (checked[i][j] != 1):
+ # Find clusters, if the pixel is above threshold
+ if (mat[i][j] >= white_threshold):
+ #print "Hit above white threshold"
+ # Add a new cluster to the list of clusters
+ cluster = []
+ clusters.append(cluster)
+ # Find the rest of the cluster
+ find_cluster(i, j)
+ #print "Number at: %dx%dpx : %s" % (j, i, mat[i][j])
+ checked[i][j] = 1
+ # If there's one or more clusters, calculate its or their balance point
+ if len(clusters) > 0:
+ balance_point = cluster_to_balance_point()
+ logging.info('%s, %s', balance_point[1], balance_point[0])
+ #print balance_point
+ # Empty the global clusters variable again
+ del clusters[:]
+ balances.append([time.time(), balance_point])
+ mean = mean_balances()
+ logging.info('%s, %s (balance mean)', mean[1], mean[0])
+ floats = coordinate_to_float(balance_point[0], balance_point[1])
+ logging.info('%s, %s (float)', floats[1], floats[0])
+ #return balance_point
+ return floats
+ else:
+ logging.error('Input matrix has wrong dimension!')
+
+
+# Find cluster around a non-black pixel
+def find_cluster(x, y):
+ global checked
+ global mat
+ global clusters
+ # Append the current white dot to the last cluster
+ dot = np.array([x, y, mat[x][y]])
+ clusters[len(clusters) - 1].append(dot)
+ # Search for surrounding white dots now
+ # Search one pixel further right
+ if (len(mat) - 1 >= (x + 1)) and (mat[x + 1][y] >= white_threshold) \
+ and (checked[x + 1][y] != 1):
+ find_cluster(x + 1, y)
+ # Search one pixel further right and down
+ if (len(mat) - 1 >= (x + 1)) and (len(mat[0]) - 1 >= y + 1) and \
+ (mat[x + 1][y + 1] >= white_threshold) \
+ and (checked[x + 1][y] != 1):
+ find_cluster(x + 1, y + 1)
+ # Search one pixel further down
+ if (len(mat[0]) - 1 >= y + 1) and \
+ (mat[x][y + 1] >= white_threshold) and (checked[x][y + 1] != 1):
+ find_cluster(x, y + 1)
+ # Search one pixel further down and further left
+ if (len(mat[0]) - 1 >= y + 1) and x - 1 >= 0 \
+ and (mat[x - 1][y + 1] >= white_threshold) \
+ and (checked[x - 1][y + 1] != 1):
+ find_cluster(x - 1, y + 1)
+ # Add this pixel to the list of checked pixels
+ checked[x][y] = 1
+
+
+# Create balance point from cluster
+# TODO: Make possible to choose only most significant cluster
+def cluster_to_balance_point():
+ global clusters
+ cluster_balances = []
+ x_balance = 0.0
+ y_balance = 0.0
+ for cluster in clusters:
+ mean_x = 0.0
+ mean_y = 0.0
+ sum_total = 0.0
+ for dot in cluster:
+ # Calculate X balance (x * intensity)
+ mean_x = mean_x + dot[0] * dot[2]
+ # Calculate Y balance (y * intensity)
+ mean_y = mean_y + dot[1] * dot[2]
+ # Calculate Y total (all intensity summed up)
+ sum_total = sum_total + dot[2]
+ # Add up the balances and put them into a list
+ cluster_x_balance = mean_x / sum_total
+ cluster_y_balance = mean_y / sum_total
+ cluster_balances.append([cluster_x_balance, cluster_y_balance])
+ # If it's more than one cluster, balance between them
+ if len(cluster_balances) > 1:
+ logging.info('Balancing between a couple of clusters.')
+ total_cluster_x_balance = 0.0
+ total_cluster_y_balance = 0.0
+ for balance in cluster_balances:
+ total_cluster_x_balance = total_cluster_x_balance + balance[0]
+ total_cluster_y_balance = total_cluster_y_balance + balance[1]
+ x_balance = total_cluster_x_balance / float(len(cluster_balances))
+ y_balance = total_cluster_y_balance / float(len(cluster_balances))
+ else:
+ logging.info('Balancing between one cluster.')
+ x_balance = cluster_x_balance
+ y_balance = cluster_y_balance
+ return [x_balance, y_balance]
+
+
+# Displays the mean balance calculated from all balances
+def mean_balances():
+ global balances
+ mean_balance = [0.0, 0.0]
+ for balance in balances:
+ mean_balance[0] = mean_balance[0] + balance[1][0]
+ mean_balance[1] = mean_balance[1] + balance[1][1]
+ mean_balance[0] = mean_balance[0] / float(len(balances))
+ mean_balance[1] = mean_balance[1] / float(len(balances))
+ return mean_balance
+
+
+# Calculates float value between 0.0 and 1.0 from coordinate
+# TODO: insert on-the-fly mean_balance as parameter
+def coordinate_to_float(x, y):
+ global mat
+ width = float(len(mat))
+ height = float(len(mat[0]))
+# balance_dim = [width / 2, height / 2]
+ floatx = x / width
+ floaty = y / height
+ return [floatx, floaty]
+# TODO: Function to calculate floats from mean_balance on the fly