Skip to content

Instantly share code, notes, and snippets.

@steveway
Last active April 19, 2017 15:19
Show Gist options
  • Select an option

  • Save steveway/a4f5f55ecd0bbe5a5a3658191b2bf310 to your computer and use it in GitHub Desktop.

Select an option

Save steveway/a4f5f55ecd0bbe5a5a3658191b2bf310 to your computer and use it in GitHub Desktop.
This loops a videofile and shows the time on top.
# To simulate netcat: https://gist.github.com/leonjza/f35a7252babdf77c8421
# Test Cython for dithering: http://docs.cython.org/en/latest/src/tutorial/numpy.html
import numpy as np
from time import localtime, strftime, time, sleep
import cv2
import sys
use_cython = True
if use_cython:
import pyximport
pyximport.install(setup_args={"include_dirs":np.get_include()}, reload_support=True)
import st_dither2 as st_dither
else:
import st_dither
def gen_bayer_matrix(n, old=None, brightness=0):
if old is None:
old = np.array([[1, 2], [3, 0]],dtype=np.float)
if len(old) >= n:
for y in range(len(old)):
for x in range(len(old)):
old[x, y] = 255 * ((old[x, y] + 0.5) / (len(old) * len(old)))
return old
new = np.zeros((len(old) * 2, len(old) * 2),dtype=np.float)
for i in range(4):
new[len(old) * (i % 2):len(old) + (len(old) * (i % 2)), len(old) * (i // 2):len(old) + (len(old) * (i // 2))] = ((4 * old + (i + 1) % 4)-brightness)
return gen_bayer_matrix(n, old=new)
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))
def main():
bayer_size = 64
brightness = 0.1
th_map = gen_bayer_matrix(bayer_size, None, brightness)
# print(th_map)
OutError = None
cap = cv2.VideoCapture(r"C:\Users\Stefan\Pictures\20160630_154705.avi")
font = cv2.FONT_HERSHEY_SIMPLEX
outfilepath = r"C:\Users\Stefan\PycharmProjects\OpenCVTest\outfile.gmv"
outfile = open(outfilepath, "wb")
while cap.isOpened():
ret, frame = cap.read()
# Loop back to the start of the video and the loop
if not ret:
cap.set(cv2.CAP_PROP_POS_MSEC, 0)
# print("Reset")
continue
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# We want the picture in portrait-orientation
(height, width) = gray.shape[:2]
if width > height:
gray = rotate_bound(gray,90)
# And it should be 600x800
if (height, width) != (800, 600):
gray = cv2.resize(gray,(600,800))
currclocktime = strftime("%X", localtime())
textdims = cv2.getTextSize(currclocktime, font, 4, 2)
textcoords = (int((gray.shape[:2][1]-textdims[0][0])/2), int((gray.shape[:2][0]-textdims[0][1])/4))
cv2.putText(gray, currclocktime, textcoords, font, 4, (255, 255, 255), 2, cv2.LINE_AA)
lastframetime = time()
gray = st_dither.apply_ordered_dither(gray, th_map, bayer_size)
# print(time()-lastframetime)
#cv2.imshow('frame', gray)
#if cv2.waitKey(1) & 0xFF == ord('q'):
# break
#if (time()-lastframetime) < (1/7.7):
# sleep((1/7.7)-(time()-lastframetime))
# Pack and save the resulting frames
outarray = gray.reshape(-1,8)
outarray = np.fliplr(outarray)
outarray = outarray * 255
outarray = np.packbits(outarray,None)
#outarray.tofile(outfile)
for i in outarray:
sys.stdout.buffer.write(i)
# This does not work with stdout
# outarray.tofile(sys.stdout)
cap.release()
cv2.destroyAllWindows()
outfile.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment