Feedback Loops with OpenCV

Last week I had some free time so I learned how to set up OpenCV, pulled out a thrift store webcam, and started tinkering. OpenCV is a suuuper cooool and powerful library that you can use to do all kinds of amazing things with robotics and security systems and smart phones and its changing the world and so of course I had to sit down with it and make my face look weird.
I played around with some of the blurs and color filtering capabilities just to kinda see how it all worked and got some promising results:
I found a setting I liked, turned the camera toward the monitor to create a loop, and got some lovely glitchy goodness...

There's additional images at the bottom of this post.

OpenCV is capable of doing a great deal more than just producing trippy images but as someone who loves trippy images, I'm quite pleased with this kind of thing. Maybe one day I'll figure out how to do all the important life changing stuff with OpenCV, but for now enjoy these bent and beautiful computer paintings.

I used python for this and if you are interested in looking at the code I used to achieve these images, see below! You'll need to have OpenCV installed though which itself can be a challenge. I found it easiest to use the Anaconda package manager which I already had on my machine.
import cv2
import random


# set frame counter

count = 0


# I have two webcams, one is connected via usb and the other is built into the monitor.
# To create a loop by turning the camera on the monitor, you will need a webcam that is separate from the monitor.

cap = cv2.VideoCapture(0) # USB webcam


while(True):
    
    # Capture frame-by-frame
    
    ret, frame = cap.read()


    # Resize playback windows, width is desired new width
    
    width = 600 # change this number to desired width
    r = width / frame.shape[1]
    height = int(frame.shape[0] * r)
    dim = (width,height)
    
    resized = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
    

    # Add effects
    
    blur = cv2.GaussianBlur(resized,(11,11),100)
    filt = cv2.bilateralFilter(blur,33,75,75)
    

    # convert to HSV from BGR
    
    hsv=cv2.cvtColor(filt, cv2.COLOR_BGR2HSV)
    

    # show the windows with the images

    cv2.imshow('orig', resized)
    cv2.imshow('result', hsv)


    # move windows so they don't overlap
    
    cv2.moveWindow('orig',0,0)
    cv2.moveWindow('result',width,0)
    

    # this will write a jpg every five frames from the result
        
    if count%5 == 0:
        cv2.imwrite('pics/opencv/capture%d.jpg'%(random.randint(1,10000000)),hsv)
        print('captured frame')
    

    count+=1


    # press 'q' to stop
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


# When everything is done, this releases the capture

cap.release()
cv2.destroyAllWindows()
cv2.waitKey(1)