Sidebar

Home



Knowledge Base


Guides & Tutorials

Projects

Samples

kb:tutorials:easy:how_to_configure_opencv_as_external_camera

How to configure OpenCV as external camera

TNG supports “External camera” to be used instead of standard web cam. With this little “hack”, video stream from external camera is first captured and processed with OpenCV using python script.


First prerequisite is that you install Python on your computer. You would also need to install OpenCV python library. Luckily, step by step installation guide for both is available at the link below:
How to install and configure Python

Now that you have installed Python and OpenCV to your computer, you need to create two files. First is the python script which handles the video image capture and sends it to TNG.

So create new text file with your text editor and name it opencv. Make sure that you save it as python (.py) file. Place this file into your profile folder.
You can copy/paste the code below:

import time
import planetcnc
import cv2
 
def OpenCV():
	planetcnc.print("OpenCV START")
	cnt = 0
	cap = cv2.VideoCapture(0)
	while(1):
 
		# Capture image
		ret, image = cap.read() 
 
		# Convert image to grayscale
		img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
		# Find canny edges
		edges = cv2.Canny(img_gray, 30, 200)
 
		# Find contours
		if cv2.getVersionMajor() in [2, 4]:
			# OpenCV2, OpenCV4 
			contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
		else:
			# OpenCV3
			image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
 
		# Draw all contours (-1 means drawing all contours)
		cv2.drawContours(image, contours, -1, (0, 255, 0), 1)
 
		# Send image to PlanetCNC TNG
		img = planetcnc.imageOpenData(image)
		planetcnc.imageToCam(img)
		planetcnc.imageClose(img)
 
		time.sleep(0.1)
		cnt += 1	
		#planetcnc.print('Count: ', cnt)		
		#if (cnt >= 100):
		#	break	
 
		opencv_show = planetcnc.getParam('_opencv_show')
		if (opencv_show != 1):
			break;
 
	cv2.destroyAllWindows()
	cap.release() 
	planetcnc.print("OpenCV END")


Now create second file, which will be expression file. Name the file Expr_opencv. This file should have .txt suffix. Place this file into your profile folder. You can copy/paste the code below:

#OnInit
_opencv_show=0;
 
#OnShutdown
_opencv_show=0;
 
#OnCamera
print('Expr: OnCamera: ', .arg1);
if(.arg1==10000 && _opencv_show==0, 
  exec(_opencv_show=1, pythr("./opencv.py|OpenCV()")),
  exec(_opencv_show=0)
 )


Open TNG and click Machine/Camera/Show. Click the button with camera icon and select External:

kb/tutorials/easy/how_to_configure_opencv_as_external_camera.txt · Last modified: 2023/12/19 20:32 by andrej

Page Tools