├── Calibration ├── Calibrator.py ├── TakeImages.py ├── param_K.npy ├── param_dist.npy ├── param_ret.npy ├── pattern.png └── pics │ ├── pic0.jpg │ ├── pic1.jpg │ ├── pic10.jpg │ ├── pic11.jpg │ ├── pic12.jpg │ ├── pic13.jpg │ ├── pic14.jpg │ ├── pic15.jpg │ ├── pic16.jpg │ ├── pic17.jpg │ ├── pic18.jpg │ ├── pic19.jpg │ ├── pic2.jpg │ ├── pic20.jpg │ ├── pic21.jpg │ ├── pic22.jpg │ ├── pic23.jpg │ ├── pic24.jpg │ ├── pic25.jpg │ ├── pic26.jpg │ ├── pic27.jpg │ ├── pic28.jpg │ ├── pic29.jpg │ ├── pic3.jpg │ ├── pic30.jpg │ ├── pic31.jpg │ ├── pic32.jpg │ ├── pic33.jpg │ ├── pic34.jpg │ ├── pic35.jpg │ ├── pic36.jpg │ ├── pic37.jpg │ ├── pic38.jpg │ ├── pic39.jpg │ ├── pic4.jpg │ ├── pic5.jpg │ ├── pic6.jpg │ ├── pic7.jpg │ ├── pic8.jpg │ └── pic9.jpg ├── Firmware_loader ├── firmware_V2.bin └── ps4eye_init.py ├── OpenCV_viewer ├── Get_Video.py ├── View_Depth.py ├── View_RGB.py ├── param_K.npy ├── param_dist.npy └── param_ret.npy ├── README.md ├── demo.gif └── trash.py /Calibration/Calibrator.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from numpy import savetxt 3 | import cv2 4 | import glob 5 | 6 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) 7 | 8 | objp = np.zeros((6*7,3), np.float32) 9 | objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2) 10 | 11 | objpoints = [] 12 | imgpoints = [] 13 | 14 | images = glob.glob('pics/*.jpg') 15 | 16 | for fname in images: 17 | img = cv2.imread(fname) 18 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 19 | 20 | ret, corners = cv2.findChessboardCorners(gray, (7,6),None) 21 | 22 | if ret == True: 23 | objpoints.append(objp) 24 | 25 | corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) 26 | imgpoints.append(corners2) 27 | 28 | ret, K, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None) 29 | 30 | with open('param_ret.npy', 'wb') as f: 31 | np.save(f, ret) 32 | 33 | with open('param_K.npy', 'wb') as f: 34 | np.save(f, K) 35 | 36 | with open('param_dist.npy', 'wb') as f: 37 | np.save(f, dist) 38 | 39 | mean_error = 0 40 | for i in range(len(objpoints)): 41 | imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], K, dist) 42 | error = cv2.norm(imgpoints[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2) 43 | mean_error += error 44 | 45 | print ("total error: ", mean_error/len(objpoints)) 46 | 47 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /Calibration/TakeImages.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | 4 | cap = cv2.VideoCapture(2) 5 | 6 | cap.set(cv2.CAP_PROP_FRAME_WIDTH, 3448) 7 | cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 808) 8 | 9 | def decode(frame): 10 | left = np.zeros((800,1264,3), np.uint8) 11 | right = np.zeros((800,1264,3), np.uint8) 12 | 13 | for i in range(800): 14 | left[i] = frame[i, 64: 1280 + 48] 15 | right[i] = frame[i, 1280 + 48: 1280 + 48 + 1264] 16 | 17 | return (left, right) 18 | 19 | print("to perform calibration make sure you have a chessboard printed and pisitoned ata clear white wall for the best results.") 20 | print("We will take 40 pictures from diffrent angles. Each time press enter to take a picture.") 21 | print("So let's get started by making the first picture") 22 | 23 | for i in range (40): 24 | input("press enter to make picture") 25 | ret, frame = cap.read() 26 | left, right = decode(frame) 27 | cv2.imwrite("pic" + str(i)+ ".jpg", left) 28 | 29 | print("done!") 30 | 31 | cap.release() 32 | -------------------------------------------------------------------------------- /Calibration/param_K.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/param_K.npy -------------------------------------------------------------------------------- /Calibration/param_dist.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/param_dist.npy -------------------------------------------------------------------------------- /Calibration/param_ret.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/param_ret.npy -------------------------------------------------------------------------------- /Calibration/pattern.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pattern.png -------------------------------------------------------------------------------- /Calibration/pics/pic0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic0.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic1.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic10.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic11.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic12.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic13.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic14.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic15.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic16.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic17.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic18.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic19.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic19.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic2.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic20.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic21.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic21.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic22.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic22.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic23.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic24.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic24.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic25.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic25.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic26.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic26.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic27.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic27.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic28.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic28.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic29.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic29.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic3.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic30.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic30.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic31.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic31.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic32.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic32.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic33.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic33.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic34.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic34.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic35.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic35.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic36.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic36.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic37.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic37.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic38.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic38.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic39.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic39.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic4.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic5.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic6.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic7.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic8.jpg -------------------------------------------------------------------------------- /Calibration/pics/pic9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Calibration/pics/pic9.jpg -------------------------------------------------------------------------------- /Firmware_loader/firmware_V2.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/Firmware_loader/firmware_V2.bin -------------------------------------------------------------------------------- /Firmware_loader/ps4eye_init.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import usb.core 3 | import usb.util 4 | import sys 5 | 6 | # check if initialized device already exists 7 | dev = usb.core.find(idVendor=0x05a9, idProduct=0x058a) 8 | if dev is not None: 9 | print('PS4 camera Version 1 already initialized') 10 | sys.exit() 11 | 12 | dev = usb.core.find(idVendor=0x05a9, idProduct=0x058b) 13 | if dev is not None: 14 | print('PS4 camera Version 2 already initialized') 15 | sys.exit() 16 | 17 | # find uninitialized device 18 | dev = usb.core.find(idVendor=0x05a9, idProduct=0x0580) 19 | if dev is None: 20 | print('PS4 camera not found') 21 | sys.exit() 22 | 23 | # set the active configuration. With no arguments, the first 24 | # configuration will be the active one 25 | dev.set_configuration() 26 | 27 | # helper function for chunking a file 28 | def read_chunks(infile, chunk_size): 29 | while True: 30 | chunk = infile.read(chunk_size) 31 | if chunk: 32 | yield chunk 33 | else: 34 | return 35 | 36 | chunk_size=512 37 | index=0x14 38 | value=0 39 | 40 | firmware=open("firmware_V2.bin","rb") 41 | 42 | # transfer 512b chunks of the firmware 43 | for chunk in read_chunks(firmware, chunk_size): 44 | ret = dev.ctrl_transfer(0x40, 0x0, value, index, chunk) 45 | value+=chunk_size 46 | if value>=65536: 47 | value=0 48 | index+=1 49 | if len(chunk)!=ret: 50 | print("sent %d/%d bytes" % (ret,len(chunk))) 51 | 52 | # command reboots device with new firmware and product id 53 | try: 54 | ret = dev.ctrl_transfer(0x40, 0x0, 0x2200, 0x8018, [0x5b]) 55 | except: 56 | print('PS4 camera firmware uploaded and device reset') 57 | -------------------------------------------------------------------------------- /OpenCV_viewer/Get_Video.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | from matplotlib import pyplot as plt 4 | 5 | cap = cv2.VideoCapture(0) 6 | cap.set(cv2.CAP_PROP_FRAME_WIDTH, 3448) 7 | cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 808) 8 | 9 | def decode(frame): 10 | left = np.zeros((800,1264,3), np.uint8) 11 | right = np.zeros((800,1264,3), np.uint8) 12 | 13 | for i in range(800): 14 | left[i] = frame[i, 64: 1280 + 48] 15 | right[i] = frame[i, 1280 + 48: 1280 + 48 + 1264] 16 | 17 | return (left, right) 18 | 19 | out_full = cv2.VideoWriter('full.avi',cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 40,(3448,808)) 20 | out_left = cv2.VideoWriter('left.avi',cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 40,(1264,800)) 21 | out_right = cv2.VideoWriter('right.avi',cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 40,(1264,800)) 22 | 23 | while(1): 24 | 25 | ret, frame = cap.read() 26 | right, left = decode(frame) 27 | 28 | out_full.write(frame) 29 | out_left.write(left) 30 | out_right.write(right) 31 | 32 | cv2.imshow('left', left) 33 | cv2.imshow('right', right) 34 | 35 | if cv2.waitKey(1) & 0xFF == ord('q'): 36 | break 37 | 38 | cap.release() 39 | cv2.destroyAllWindows() 40 | -------------------------------------------------------------------------------- /OpenCV_viewer/View_Depth.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | from matplotlib import pyplot as plt 4 | import time 5 | 6 | #Load camera parameters 7 | ret = np.load('param_ret.npy') 8 | K = np.load('param_K.npy') 9 | dist = np.load('param_dist.npy') 10 | h,w = (800, 1264) 11 | new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(K,dist,(w,h),1,(w,h)) 12 | 13 | mapx, mapy = cv2.initUndistortRectifyMap(K,dist, None ,new_camera_matrix,(w, h),cv2.CV_16SC2) 14 | 15 | #cap = cv2.VideoCapture("/home/sieuwe/Desktop/vidz/full_2.avi") 16 | #cap = cv2.VideoCapture("/home/sieuwe/Desktop/vidz/full_3.avi") 17 | #cap = cv2.VideoCapture("/home/sieuwe/Desktop/vidz/full_1.avi") 18 | #cap = cv2.VideoCapture(2) 19 | cap.set(cv2.CAP_PROP_FRAME_WIDTH, 3448) 20 | cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 808) 21 | 22 | kernel= np.ones((13,13),np.uint8) 23 | 24 | #Stereo matcher settings 25 | win_size = 5 26 | min_disp = 10 27 | max_disp = 16 * 2 + 10 28 | num_disp = max_disp - min_disp # Needs to be divisible by 16 29 | stereo = cv2.StereoSGBM_create(minDisparity= min_disp, 30 | numDisparities = num_disp, 31 | blockSize = 5, 32 | uniquenessRatio = 10, 33 | speckleWindowSize = 1000, 34 | speckleRange = 10, 35 | disp12MaxDiff = 25, 36 | P1 = 8*3*win_size**2,#8*3*win_size**2, 37 | P2 =32*3*win_size**2) #32*3*win_size**2) 38 | 39 | def decode(frame): 40 | left = np.zeros((800,1264,3), np.uint8) 41 | right = np.zeros((800,1264,3), np.uint8) 42 | 43 | for i in range(800): 44 | left[i] = frame[i, 64: 1280 + 48] 45 | right[i] = frame[i, 1280 + 48: 1280 + 48 + 1264] 46 | 47 | return (left, right) 48 | 49 | def get_distance(d): 50 | return 30 * 10/d 51 | 52 | while(1): 53 | 54 | ret, frame = cap.read() 55 | 56 | start = time.time() 57 | 58 | right, left = decode(frame) 59 | 60 | #Undistort images 61 | #img_1_undistorted = cv2.undistort(left, K, dist, None, new_camera_matrix) 62 | #img_2_undistorted = cv2.undistort(right, K, dist, None, new_camera_matrix) 63 | #img_1_undistorted= cv2.remap(left,mapx,mapy, cv2.INTER_LANCZOS4, cv2.BORDER_CONSTANT, 0) 64 | #img_2_undistorted= cv2.remap(right,mapx,mapy, cv2.INTER_LANCZOS4, cv2.BORDER_CONSTANT, 0) 65 | 66 | #downsample image for higher speed 67 | img_1_downsampled = cv2.pyrDown(cv2.cvtColor(left, cv2.COLOR_BGR2GRAY)) 68 | img_2_downsampled = cv2.pyrDown(cv2.cvtColor(right, cv2.COLOR_BGR2GRAY)) 69 | 70 | new_w, new_h = img_1_downsampled.shape 71 | 72 | #compute stereo 73 | disp = stereo.compute(img_1_downsampled,img_2_downsampled) 74 | 75 | #denoise step 1 76 | denoised = ((disp.astype(np.float32)/ 16)-min_disp)/num_disp 77 | dispc= (denoised-denoised.min())*255 78 | dispC= dispc.astype(np.uint8) 79 | 80 | #denoise step 2 81 | denoised= cv2.morphologyEx(dispC,cv2.MORPH_CLOSE, kernel) 82 | 83 | #apply color map 84 | disp_Color= cv2.applyColorMap(denoised,cv2.COLORMAP_OCEAN) 85 | 86 | f = 0.3*w # 30cm focal length 87 | Q = np.float32([[1, 0, 0, -0.5*new_w], 88 | [0,-1, 0, 0.5*new_h], # turn points 180 deg around x-axis, 89 | [0, 0, 0, f], # so that y-axis looks up 90 | [0, 0, 1, 0]]) 91 | points = cv2.reprojectImageTo3D(disp, Q) 92 | 93 | z_values = points[:,:,2] 94 | z_values = z_values.flatten() 95 | indices = z_values.argsort() 96 | 97 | precentage = 25280 98 | min_distance = np.mean(np.take(z_values,indices[0:precentage])) # takes the 30% lowest measuerements and gets the average distance from these. 99 | avg_distance = np.mean(z_values) # averages all distances 100 | max_distance = np.mean(np.take(z_values,indices[z_values.shape[0]-precentage:z_values.shape[0]])) # takes the 30% highest measuerements and gets the average distance from these. 101 | #print(np.take(z_values,indices[z_values.shape[0]-precentage:z_values.shape[0]])) 102 | #print(np.take(z_values,indices[:-100])) 103 | 104 | #visualize 105 | cv2.imshow("Depth", disp_Color) 106 | left = cv2.resize(left,(632, 400)) 107 | color_depth = cv2.addWeighted(left,0.4,disp_Color,0.4,0) 108 | 109 | end = time.time() 110 | fps = 1 / (end-start) 111 | 112 | cv2.putText(color_depth, "minimum: " + str(round(min_distance,1)),(5, 20),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2,cv2.LINE_AA) 113 | cv2.putText(color_depth, "average: " + str(round(avg_distance,1)),(5, 40),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2,cv2.LINE_AA) 114 | cv2.putText(color_depth, "maximum: " + str(round(max_distance,1)),(5, 60),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2,cv2.LINE_AA) 115 | cv2.putText(color_depth, "FPS: " + str(round(fps)),(5, 80),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2,cv2.LINE_AA) 116 | 117 | cv2.imshow("color & Depth", color_depth) 118 | 119 | if cv2.waitKey(1) & 0xFF == ord('q'): 120 | break 121 | 122 | cap.release() 123 | cv2.destroyAllWindows() 124 | -------------------------------------------------------------------------------- /OpenCV_viewer/View_RGB.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | 4 | #cap = cv2.VideoCapture('http://root:root@192.168.70.52/mjpg/1/video.mjpg') 5 | cap = cv2.VideoCapture(2) 6 | 7 | cap.set(cv2.CAP_PROP_FRAME_WIDTH, 3448) 8 | cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 808) 9 | 10 | def decode(frame): 11 | left = np.zeros((800,1264,3), np.uint8) 12 | right = np.zeros((800,1264,3), np.uint8) 13 | 14 | for i in range(800): 15 | left[i] = frame[i, 64: 1280 + 48] 16 | right[i] = frame[i, 1280 + 48: 1280 + 48 + 1264] 17 | 18 | return (left, right) 19 | 20 | while(True): 21 | 22 | ret, frame = cap.read() 23 | #cv2.normalize(frame, frame, 0, 255, cv2.NORM_MINMAX) 24 | 25 | left, right = decode(frame) 26 | 27 | cv2.imshow('left',left) 28 | cv2.imshow('right',right) 29 | 30 | if cv2.waitKey(1) & 0xFF == ord('q'): 31 | break 32 | 33 | cap.release() 34 | cv2.destroyAllWindows() 35 | -------------------------------------------------------------------------------- /OpenCV_viewer/param_K.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/OpenCV_viewer/param_K.npy -------------------------------------------------------------------------------- /OpenCV_viewer/param_dist.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/OpenCV_viewer/param_dist.npy -------------------------------------------------------------------------------- /OpenCV_viewer/param_ret.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/OpenCV_viewer/param_ret.npy -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PS4 camera: The cheap high resolution 3D stereo camera for OpenCV and Python! 2 | 3 | ![](https://github.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/blob/main/demo.gif) 4 | 5 | [![Demo](https://img.youtube.com/vi/c7CF6eDC0_A/0.jpg)](https://www.youtube.com/watch?v=c7CF6eDC0_A) 6 | 7 | ## Contents 8 | 9 | ### 1: Intro 10 | ### 2: Installation 11 | ### 3: Changing Video Settings 12 | ### 4: Camera specifications 13 | ### 5: References 14 | 15 | ## 1: Intro 16 | Since the launch of the Xbox Kinect, a lot of researchers have used it for robotics and computer vision projects because of its low price while showing great performance. Lately however the Xbox kinect hardware is getting outdated with its low resolution RGB camera and IR based depth perception system which is sensative for other IR light sources. This sensitivty to other IR light sources for examples renders the xbox kinect compelty useless for outdoor projects. Because of these limitations a replecement is needed. This is where the PS4 camera comes into play. 17 | 18 | The PS4 camera has some great hardware upgrades compared to the xbox kinect and is also availbile for a low price. The PS4 camera has a pair of much better 1280x800 resolution cameras paired with a high speed controller. This makes the latency around 120 ~ 150ms. Because of its two cameras the PS4 camera can perceive depth using stereo vision instead of a IR pattern. This makes the PS4 camera also suitable for outdoor projects. The PS4 camera depth image has also a higher resultion which makes it capable of capturaring more detail. 19 | 20 | This repo contains code for using both the PS4 V1(CUH-ZEY1) camera or PS4 V2(CUH-ZEY2) camera with linux. The code in this repo consists a initialization folder which contains code for uploading firmware to the PS4 camera. It also contains code for recieving, viewing and using the RGB and depth stream of the PS4 camera in OpenCV using Python. 21 | 22 | ## 2: Installation 23 | 24 | ### Hardware 25 | To connect the PS4 camera to a computer first the cable needs to be modded. This is because the PS4 camera has a proprotery connector called the PS4 "AUX" connector. This is actually just a ordinary USB 3.0 cable with a special connector. Because this connector wont fit in a USB 3.0 port of your linux system we need to mod it. This needs to be done by cutting a USB 3.0 cable which has a USB 3.0 type A connector. Also the AUX port on PS4 camera needs to be cut off. After this the USB 3.0 type A connector needs to be solderd to the PS4 camera cable using the colors in the cable. If you have the adapter that allows you to connect the camera to a PS5 using a standard USB 3 connector, this can be used as an alternative to modifying the camera. 26 | 27 | For more detailed instructions you can visit: 28 | https://www.instructables.com/HACK-PlayStation-4-Cam-Into-Cheap-3D-Depth-Camera-/ 29 | 30 | - To check if the cable is made correctly: 31 | 32 | - Type in a Terminal ```lsusb``` 33 | 34 | - This should show a device with the name ```OmniVision Technologies, Inc.``` 35 | 36 | 37 | ### Software 38 | Step 1: 39 | - First we need to install pysub. 40 | - ```sudo pip install --pre pyusb``` 41 | 42 | 43 | Step 2: 44 | - Now we need to initialize the camera by uploading a modded firmware to the PS4 camera. 45 | - ```cd Firmware_loader``` 46 | - ```sudo python3 ps4eye_init.py``` 47 | - Select your camera version by typing ```1``` or ```2``` 48 | - The program should finish with ```PS4 camera firmware uploaded and device reset``` 49 | 50 | 51 | Step 3: 52 | - Now we need to know at what index the camera has been initalized. 53 | - Type in a Terminal ```v4l2-ctl --list-devices``` 54 | - This should show a device named ```USB Camera-OV580: USB Camera-OV (usb-0000:00:14.0-2):``` 55 | - This device will have two ```/dev/videoX``` locations. 56 | - The number behind ```video``` will indicate the index. The lowest number of the two is the index we need. 57 | - Go to ```View_RGB.py``` and ```View_Depth.py``` and change this line with the index number ```cap = cv2.VideoCapture(index number here)``` 58 | 59 | 60 | Step 4: 61 | Now we can view the Depth or RGB image from the camera. 62 | 63 | - ```cd OpenCV_viewer``` 64 | 65 | - For RGB run ```python3 View_RGB.py``` 66 | - For Depth run ```python3 View_Depth.py``` 67 | 68 | - If OpenCV gives a error try changing the index number from Step 3 69 | 70 | ## 3: Changing Video Settings 71 | After using the camera you may find the default video settings of the camera insufficient. The image may be to dark inside or the resolution is not the one you want to use. These settings can be changed by following the steps below. 72 | 73 | To change video settings we first need to install the ```v4l2-utils``` package. We can do this with the following command. 74 | - ```sudo apt install v4l2-utils``` 75 | 76 | Now we can view all available settings with. 77 | - ```v4l2-ctl -l``` 78 | 79 | To change for example the exposure we can use the following command. 80 | - ```v4l2-ctl exposure = 1``` 81 | 82 | ## 4: Camera specifications 83 | - Latency 120~150ms 84 | - External Dimensions: Approx. 186mm x 27mm x 27mm (width x height x depth) 85 | - Weight: Approx. 183g 86 | - Video Pixel: (Maximum) 1280 x 800 pixel x 2 87 | - Video Frame Rate: 1280x800 pixel @ 60fps 88 | 640x400 pixel @ 120fps 89 | 320x192 pixel @ 240fps 90 | - Video Format: RAW YUV (uncompressed) 91 | - Lens: Dual Lenses, F value/F2.0 fixed focus 92 | - Capture Range: 30cm~ 93 | - Field-of-View: 85 degrees 94 | - Microphone: 4 Channel Microphone Array 95 | - Connection Type: USB3.0 96 | - Cable Length: Approx. 2m 97 | 98 | ## 5: References 99 | - This project is a compilation of the amazing work of other people. Big thanks to everyone listed below: 100 | - https://github.com/bigboss-ps3dev 101 | - https://github.com/ps4eye 102 | -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sieuwe1/PS4-eye-camera-for-linux-with-python-and-OpenCV/4180061a1afd109a9204557f256d341c5850c124/demo.gif -------------------------------------------------------------------------------- /trash.py: -------------------------------------------------------------------------------- 1 | #dispc= (denoised-denoised.min())*255 2 | #dispC= dispc.astype(np.uint8) # Convert the type of the matrix from float32 to uint8, this way you can show the results with the function cv2.imshow() 3 | #dispC_inverted = cv2.bitwise_not(dispC) 4 | #mask = np.zeros(dispC.shape, np.uint8) 5 | #dispC_inverted = np.where(dispC_inverted==255, dispC_inverted, 0) 6 | 7 | #blurred_img = cv2.GaussianBlur(dispC, (11,11), 0) 8 | #contours, hierarchy = cv2.findContours(dispC_inverted, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 9 | #cv2.drawContours(mask, contours, -1, (255,255,255),10) 10 | #output = np.where(mask!=255, dispC, 80) --------------------------------------------------------------------------------