Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 2088

Camera freezes after few frames

$
0
0
When I run the code the camera only shows the first frames and then it freezes, how can I make the code faster? I think the main problem is the "for" loop because it iterates over a hundred "cnt". import cv2 import numpy as np from serial.tools import list_ports from pydobot import Dobot import csv import time from PIL import Image from imutils.video import FileVideoStream from imutils.video import FPS import imutils import LinearRegression as regression def apri_chela(device): device.grip(enable=False) def chiudi_chela(device): device.grip(enable=True) def posizione_iniziale(): initial_x = 200.0 initial_y = -120.0 initial_z = 160.0 device.move_to(initial_x, initial_y, initial_z, 0.0, True) def posizione_intermedia(): trans_x = 200.0 trans_y = 0.0 trans_z = 170.0 device.move_to(trans_x, trans_y, trans_z, 0.0, True) def trova_contorni(frame): belt = frame[145:440, :] gray_belt = cv2.cvtColor(belt, cv2.COLOR_BGR2GRAY) #camera dobot 80,255 _, threshold = cv2.threshold(gray_belt, 20, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) return contours, threshold, belt def prendi(x,y): #device.conveyor_belt(0) posizione_intermedia() apri_chela(device) device.move_to(x, y, 30.0, 0.0, True) chiudi_chela(device) time.sleep(5) posizione_iniziale() def shape_detector(approx): if len(approx) == 3: shape = "Triangle" elif len(approx) == 4: shape = "Rectangle" elif len(approx) == 5: shape = "Pentagon" elif 6 < len(approx) < 15: shape = "Ellipse" else: shape = "Circle" return shape def trova_colori(frame): frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) rows,cols,_ = frame.shape for x in range(rows): for y in range(cols): pixel = frame[x,y] hue = pixel[0] saturation = pixel[1] lightness = pixel[2] if lightness <= 127.5: frame[x,y][0] = 0 elif lightness >127.5: frame[x,y][2] = 225 if saturation <= 127.5: frame[x,y][1] = 0 elif saturation >127.5: frame[x,y][1] = 225 cv2.imshow('enhanced', frame) # hsv [hue, saturation, lightness] lower_red = np.array([0, 0, 0]) # lower range for red values upper_red = np.array([255, 150, 150]) # upper range for red values mask = cv2.inRange(frame, lower_red, upper_red) #result = cv2.bitwise_and(frame,frame,mask=mask) frame[mask==0] = [0,0,0] frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR) return frame # start the file video stream thread and allow the buffer to # start to fill fvs = FileVideoStream(1).start() time.sleep(1) # start the FPS timer fps = FPS().start() sub_background = cv2.createBackgroundSubtractorMOG2(history=0, varThreshold=444, detectShadows=False) try: port = list_ports.comports()[0].device device = Dobot(port=port) pose = device.get_pose() position = pose.position posizione_iniziale() print('Ho trovato un Dobot :)') except: print("Non ho trovato nessun Dobot :(") regressore_x = regression.crea_regressore_x() regressore_y = regression.crea_regressore_y() # loop over frames from the video file stream while fvs.more(): frame = fvs.read() trasformed = trova_colori(frame) cv2.imshow('trasformed', trasformed) contours, threshold, belt = trova_contorni(frame) for cnt in contours: (x, y, w, h) = cv2.boundingRect(cnt) area = cv2.contourArea(cnt) approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True) shape = shape_detector(approx) if area > 3000: x_centro = int(x + (w/2)) y_centro = int(y + (h/2)) cv2.rectangle(belt, (x, y), (x + w, y + h), (0, 0, 255), 2) cv2.circle(belt, (x_centro, y_centro), 2,(0, 128, 255), 2) cv2.putText(belt, str(shape), (x, y), 1, 1, (0, 255, 0)) if cv2.waitKey(1) & 0xFF == ord('x'): if shape == 'Rectangle': x = regression.x_predizione(regressore_x, x_centro, y_centro) y = regression.y_predizione(regressore_y, x_centro, y_centro) prendi(x,y) cv2.imshow("frame", frame) cv2.imshow("threshold", threshold) if cv2.waitKey(1) & 0xFF == ord('q'): break fps.update() # stop the timer fps.stop() # do a bit of cleanup cv2.destroyAllWindows() fvs.stop() def utility(): print('x dell oggetto in pixel ', (x_centro)) print('y dell oggetto in pixel ', (y_centro)) pose = device.get_pose() position = pose.position print('Dobot', position)

Viewing all articles
Browse latest Browse all 2088

Trending Articles