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_CAM.py. Make sure that you save it as python (.py) file. Place this file into your profile Python folder.
You can copy/paste the code below:

#! /usr/bin/env python
 
import cv2
import numpy as np
import planetcnc
 
def start_opencv():
 
	# Open PlanetCNC camera window - OnCqamera event already did this
	#id = planetcnc.cmd_get_id("Machine.Camera.Show")
	#if not planetcnc.cmd_exec(id, 10000):
	#	print("Failed to camera window")
	#	return
 
	#cap = cv2.VideoCapture(0)
	#cap = cv2.VideoCapture(0, cv2.CAP_MSMF) #Windows (def) - might be slow
	cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) #Windows - prefered on Windows
	#cap = cv2.VideoCapture(0, cv2.CAP_V4L2) #Linux (def)
 
	if not cap.isOpened():
		print("Error: Could not open camera.")
		return
 
	while(1): 
		# Capture image
		ret, image = cap.read() 
		if not ret:
			print("Error: Could not read frame from camera.")
			break
 
		# Convert image to grayscale
		img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
		# Find canny edges
		edges = cv2.Canny(img_gray, 30, 200)
 
		# Find contours
		contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
 
		# Get image center
		h, w = image.shape[:2]
		img_center = (w // 2, h // 2)
 
		closest_contour = None
		min_distance = float("inf")
		closest_coords = (0, 0)
 
		cX = 0
		cY = 0;
		for contour in contours:
			# Compute centroid using image moments
			M = cv2.moments(contour)
			if M["m00"] != 0:
				cX = int(M["m10"] / M["m00"])
				cY = int(M["m01"] / M["m00"])
 
			# Compute Euclidean distance to the image center
			distance = np.sqrt((cX - img_center[0]) ** 2 + (cY - img_center[1]) ** 2)
 
			# Check if this is the closest object
			if distance < min_distance:
				min_distance = distance
				closest_contour = contour
				closest_coords = (cX, cY)
 
		# Draw the closest object
		if closest_contour is not None:
			cv2.drawContours(image, [closest_contour], -1, (0, 255, 0), 2)
 
			# Draw text next to contour
			rel_x, rel_y = closest_coords[0] - img_center[0], closest_coords[1] - img_center[1]  # Compute relative coordinates
			text = f"({rel_x}, {rel_y})"
			cv2.putText(image, text, (closest_coords[0] + 10, closest_coords[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
 
 
		# Convert image to PlanetCNC Image
		img = planetcnc.image_open_data(image, True)
 
		if not img:
			print("Failed to open image")
			break
 
		# Send image to PlaneCNC
		if not planetcnc.image_to_cam(img):
			planetcnc.image_close(img)
			print("Failed to send image to camera panel")
			break
 
		# Close the image
		planetcnc.image_close(img)
 
		# Read message from PlanetCNC
		msg = {}
		if planetcnc.read_message(msg):
			print("Received ", msg)
			if msg["OpenCV"] == 0:
				print("Received EXIT message")
				break
 
		# Check for termination
		if planetcnc.terminate():
			print("Termination signal received. Exiting loop.")
			break
 
		# Wait for a key press (1ms delay)
		key = cv2.waitKey(1)
 
 
	cv2.destroyAllWindows()
	cap.release() 
 
 
# Run function
start_opencv()


Now create second file, which will be expression file. Name the file Expr_OpenCV.txt. Place this file into your profile Expr folder. You can copy/paste the code below:

#OnCamera
print("#OnCamera: ", .arg1);
 
if(.arg1 == 10000, 
  py("Python/OpenCV_CAM.py", 0),
  py_msg("OpenCV", 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: by andrej

Page Tools