diff --git a/scripts/opencv/README b/scripts/opencv/README new file mode 100644 index 0000000..a9a7738 --- /dev/null +++ b/scripts/opencv/README @@ -0,0 +1,2 @@ +This dir contains some quick&dirty live camera examples for the 31C3 display in the entrance area. +They use OpenCV to grab webcam frames and either binarize them or detect edges and then display the result on the flipdot displays. diff --git a/scripts/opencv/edge.py b/scripts/opencv/edge.py new file mode 100755 index 0000000..353aa0f --- /dev/null +++ b/scripts/opencv/edge.py @@ -0,0 +1,84 @@ +#!/usr/bin/python2 +import numpy as np +import cv2 +import socket,time,math + +cap = cv2.VideoCapture(0) + + +sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) + +FD_LEFT="2001:67c:20a1:1095:c49d:e22d:6891:dcd" +FD_MIDDLE="2001:67c:20a1:1095:34b1:6957:8ddb:3a79" +FD_RIGHT="2001:67c:20a1:1095:552a:1594:871f:d9c2" + + +UDPPORT=2323 + +def send(image,dest): + msg = ''; + pieces = ''; + for line in image: + pieces += ''.join(str(x) for x in line) + + pieces = [pieces[i:i+8] for i in range(0, len(pieces), 8)] + + for i in pieces: + if (len(i) < 8): + i = i.ljust(8, '1') + msg += chr(int(str(i), 2)) + + sock.sendto(msg, (dest, UDPPORT)) + +def partition(img): + rows,cols = img.shape + + dst = cv2.transpose(img) + + cv2.imshow("rot",dst) + + left = dst[0:cols/3,0:rows] + middle = dst[cols/3+1:2*cols/3,0:rows] + right = dst[2*cols/3+1:cols,0:rows] + +# left = img[0:img.shape[0],0:img.shape[1]/3] +# middle = img[0:img.shape[0],img.shape[1]/3+1:2*img.shape[1]/3] +# right = img[0:img.shape[0],2*img.shape[1]/3+1:img.shape[1]] + return left,middle,right + + + + +while(True): + # Capture frame-by-frame + ret, frame = cap.read() + + dim=(144,120) + resized=cv2.resize(frame,dim) + + resized = cv2.flip(resized,0) + + # Our operations on the frame come here + gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) + + canny = cv2.Canny(gray,50,200) + thresh = (255-canny)/255 + + # Display the resulting frame + cv2.imshow('frame',gray) + cv2.imshow('canny',thresh*255) + left,middle,right = partition(thresh) + send(left,FD_LEFT) + send(middle,FD_MIDDLE) + send(right,FD_RIGHT) + cv2.imshow("left",left*255) + cv2.imshow("middle",middle*255) + cv2.imshow("right",right*255) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + time.sleep(0.7) + +# When everything done, release the capture +cap.release() +cv2.destroyAllWindows() diff --git a/scripts/opencv/scroll_image.py b/scripts/opencv/scroll_image.py new file mode 100755 index 0000000..5404ad5 --- /dev/null +++ b/scripts/opencv/scroll_image.py @@ -0,0 +1,117 @@ +#!/usr/bin/python2 +import numpy as np +import cv2 +import socket,time,math +import sys + + + +sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) + +FD_LEFT="2001:67c:20a1:1095:c49d:e22d:6891:dcd" +FD_MIDDLE="2001:67c:20a1:1095:34b1:6957:8ddb:3a79" +FD_RIGHT="2001:67c:20a1:1095:552a:1594:871f:d9c2" + +UDPPORT=2323 + +IMG_THRESHOLD=128 + + + +def send(image,dest): + msg = ''; + pieces = ''; + for line in image: + pieces += ''.join(str(x) for x in line) + + pieces = [pieces[i:i+8] for i in range(0, len(pieces), 8)] + + for i in pieces: + if (len(i) < 8): + i = i.ljust(8, '1') + msg += chr(int(str(i), 2)) + + #sock.sendto(msg, (dest, UDPPORT)) + +def partition(img): + rows,cols = img.shape + + dst = cv2.transpose(img) + + cv2.imshow("rot",dst) + + left = dst[0:cols/3,0:rows] + middle = dst[cols/3+1:2*cols/3,0:rows] + right = dst[2*cols/3+1:cols,0:rows] + +# left = img[0:img.shape[0],0:img.shape[1]/3] +# middle = img[0:img.shape[0],img.shape[1]/3+1:2*img.shape[1]/3] +# right = img[0:img.shape[0],2*img.shape[1]/3+1:img.shape[1]] + return left,middle,right + + +frameSkip = 0 +thresholdRadius = 75 + +img = cv2.imread(sys.argv[1]) + + +r = 120 /float(img.shape[0]) +dim = (int(img.shape[1] * r),120) +if dim[0] < 144: + sys.exit(1) + +offset = 0 + +print img.shape,r, dim +fullImage = cv2.resize(img,dim) + +cv2.imshow("resized",fullImage) + +done = False +while not done: #offset+144 < fullImage.shape[1]: + dim=(144,120) + + print fullImage.shape + if offset + 144 > fullImage.shape[1]: + offset = fullImage.shape[1] - 144 + done = True + disp = fullImage[0:120,offset:offset+144] + + resized = cv2.flip(disp,0) + + # Our operations on the frame come here + gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) + + #thresh = cv2.adaptiveThreshold(gray,1,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,thresholdRadius,-2) + ret,thresh = cv2.threshold(gray,IMG_THRESHOLD,1,cv2.THRESH_BINARY) + + # Display the resulting frame + cv2.imshow('frame',gray) + cv2.imshow('canny',thresh*255) + left,middle,right = partition(thresh) + send(left,FD_LEFT) + send(middle,FD_MIDDLE) + send(right,FD_RIGHT) + cv2.imshow("left",left*255) + cv2.imshow("middle",middle*255) + cv2.imshow("right",right*255) + + key = cv2.waitKey(2050) + + if key & 0xFF == ord(' '): + frameSkip = 30*120 + + if key & 0xFF == ord('w'): + thresholdRadius = thresholdRadius + 2 + print "new thresh radius: ",thresholdRadius + if key & 0xFF == ord('s'): + thresholdRadius = thresholdRadius - 2 + print "new thresh radius: ",thresholdRadius + + + if key & 0xFF == ord('q'): + break + + offset += 20 + diff --git a/scripts/opencv/sendxkcd.sh b/scripts/opencv/sendxkcd.sh new file mode 100755 index 0000000..776abf2 --- /dev/null +++ b/scripts/opencv/sendxkcd.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +while true +do + ./xkcd.py + ../display foo.png + + sleep 10s +done diff --git a/scripts/opencv/threshold-xkcd.py b/scripts/opencv/threshold-xkcd.py new file mode 100644 index 0000000..ed26632 --- /dev/null +++ b/scripts/opencv/threshold-xkcd.py @@ -0,0 +1,86 @@ +#!/usr/bin/python3 +import numpy as np +import cv2 +import socket,time,math +from xkcd import grabber + +sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) + +FD_LEFT="2001:67c:20a1:1095:c49d:e22d:6891:dcd" +FD_MIDDLE="2001:67c:20a1:1095:34b1:6957:8ddb:3a79" +FD_RIGHT="2001:67c:20a1:1095:552a:1594:871f:d9c2" + +UDPPORT=2323 + +IMG_THRESHOLD=64 + + + +def send(image,dest): + msg = ''; + pieces = ''; + for line in image: + pieces += ''.join(str(x) for x in line) + + pieces = [pieces[i:i+8] for i in range(0, len(pieces), 8)] + + for i in pieces: + if (len(i) < 8): + i = i.ljust(8, '1') + msg += chr(int(str(i), 2)) + if (len(i) < 8): + i = i.ljust(8, '1') + + sock.sendto(msg, (dest, UDPPORT)) + +def partition(img): + rows,cols = img.shape + + dst = cv2.transpose(img) + + cv2.imshow("rot",dst) + + left = dst[0:cols/3,0:rows] + middle = dst[cols/3+1:2*cols/3,0:rows] + right = dst[2*cols/3+1:cols,0:rows] + +# left = img[0:img.shape[0],0:img.shape[1]/3] +# middle = img[0:img.shape[0],img.shape[1]/3+1:2*img.shape[1]/3] +# right = img[0:img.shape[0],2*img.shape[1]/3+1:img.shape[1]] + return left,middle,right + + + + +while(True): + # Capture frame-by-frame + ret, frame = cv2.imread(grabber.getImage()) + + dim=(144,120) + resized=cv2.resize(frame,dim) + + resized = cv2.flip(resized,0) + + # Our operations on the frame come here + gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) + + ret,thresh = cv2.threshold(gray,IMG_THRESHOLD,1,cv2.THRESH_BINARY) + + # Display the resulting frame + cv2.imshow('frame',gray) + cv2.imshow('canny',thresh*255) + left,middle,right = partition(thresh) + send(left,FD_LEFT) + send(middle,FD_MIDDLE) + send(right,FD_RIGHT) + cv2.imshow("left",left*255) + cv2.imshow("middle",middle*255) + cv2.imshow("right",right*255) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + time.sleep(5) + +# When everything done, release the capture +cap.release() +cv2.destroyAllWindows() diff --git a/scripts/opencv/threshold.py b/scripts/opencv/threshold.py new file mode 100755 index 0000000..5fbb62f --- /dev/null +++ b/scripts/opencv/threshold.py @@ -0,0 +1,86 @@ +#!/usr/bin/python2 +import numpy as np +import cv2 +import socket,time,math + +cap = cv2.VideoCapture(0) + + +sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) + +FD_LEFT="2001:67c:20a1:1095:c49d:e22d:6891:dcd" +FD_MIDDLE="2001:67c:20a1:1095:34b1:6957:8ddb:3a79" +FD_RIGHT="2001:67c:20a1:1095:552a:1594:871f:d9c2" + +UDPPORT=2323 + +IMG_THRESHOLD=64 + + + +def send(image,dest): + msg = ''; + pieces = ''; + for line in image: + pieces += ''.join(str(x) for x in line) + + pieces = [pieces[i:i+8] for i in range(0, len(pieces), 8)] + + for i in pieces: + if (len(i) < 8): + i = i.ljust(8, '1') + msg += chr(int(str(i), 2)) + + sock.sendto(msg, (dest, UDPPORT)) + +def partition(img): + rows,cols = img.shape + + dst = cv2.transpose(img) + + cv2.imshow("rot",dst) + + left = dst[0:cols/3,0:rows] + middle = dst[cols/3+1:2*cols/3,0:rows] + right = dst[2*cols/3+1:cols,0:rows] + +# left = img[0:img.shape[0],0:img.shape[1]/3] +# middle = img[0:img.shape[0],img.shape[1]/3+1:2*img.shape[1]/3] +# right = img[0:img.shape[0],2*img.shape[1]/3+1:img.shape[1]] + return left,middle,right + + + + +while(True): + # Capture frame-by-frame + ret, frame = cap.read() + + dim=(144,120) + resized=cv2.resize(frame,dim) + + resized = cv2.flip(resized,0) + + # Our operations on the frame come here + gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) + + ret,thresh = cv2.threshold(gray,IMG_THRESHOLD,1,cv2.THRESH_BINARY) + + # Display the resulting frame + cv2.imshow('frame',gray) + cv2.imshow('canny',thresh*255) + left,middle,right = partition(thresh) + send(left,FD_LEFT) + send(middle,FD_MIDDLE) + send(right,FD_RIGHT) + cv2.imshow("left",left*255) + cv2.imshow("middle",middle*255) + cv2.imshow("right",right*255) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + time.sleep(0.55) + +# When everything done, release the capture +cap.release() +cv2.destroyAllWindows() diff --git a/scripts/opencv/xkcd.py b/scripts/opencv/xkcd.py new file mode 100755 index 0000000..646a4b7 --- /dev/null +++ b/scripts/opencv/xkcd.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + + +from bs4 import BeautifulSoup +import urllib +import os +import yaml + +INFO = "INFO" +ERROR = "ERROR" +log_level = INFO + +def log(level, msg): + if log_level == INFO: + print(level + ': ' + msg) + elif log_level == ERROR: + if level == ERROR: + print(level + ': ' + msg) + +class grabber(object): + def getImage(): + + while(True): + url = 'http://c.xkcd.com/random/comic' + found = False + + with urllib.request.urlopen(url) as conn: + page = conn.read() + + soup = BeautifulSoup(page) + image_tags = soup.find_all('img') + + + for img in image_tags: + # img[src] is the contents of the src field + # http://imgs.xkcd.com/comics/xxxx.png + if 'comics' in img['src']: + log(INFO, 'Getting ' + img['src']) + + filename = '' + if '.jpg' in img['src']: + filename = 'foo.jpg' + elif '.png' in img['src']: + filename = 'foo.png' + found = True + else: + log(ERROR, 'Did not recognize image format: ' + img['src']) + break + if(found): + urllib.request.urlretrieve(img['src'], filename) + return filename + +grabber.getImage() \ No newline at end of file