First Process I did this
import numpy as np
import cv2
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import numpy as np
img = cv2.imread("img.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.array(img, dtype=np.float64) / 255
plt.imshow(img)
Then I took the image data to train to find the mean color by k-mean.
w, h, d = original_shape = tuple(img.shape)
print(w, h, d) # 627 783 3
img = np.reshape(img, (w * h, d))
img.shape # (490941, 3)
bit_of_color = 32
kmeans = KMeans(n_clusters=bit_of_color, random_state=0).fit(img)
labels = kmeans.predict(img)
kmeans.labels_ # array([16, 16, 16, ..., 28, 28, 28], dtype=int32)
After that, I created an image to display the color obtained from the model.
image = np.zeros((w, h, d))
mean_ = kmeans.cluster_centers_
d = mean_.shape[1]
mean_[1]
def adjust_image(mean_color_from_model, labels, w, h):
d = mean_color_from_model.shape[1]
print(mean_color_from_model.shape)
image = np.zeros((w, h, d))
label_idx = 0
for i in range(w):
for j in range(h):
image[i][j] = mean_color_from_model[labels[label_idx]]
# print(image[i][j])
label_idx += 1
print(label_idx)
return image
plt.axis('off')
img_kmean = adjust_image(kmeans.cluster_centers_, labels, w, h)
plt.imshow(img_kmean)
And get the following result.
enter image description here
Next, I'll remove the color from the image and still the only the line of the object.
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('img2.png',0)
edges = cv.Canny(img,100,200)
y, x = edges.shape
for i in range(y):
for j in range(x):
if (edges[i][j] == 0):
edges[i][j] = 255
elif (edges[i][j] == 255):
edges[i][j] = 0
plt.imshow(edges,cmap = 'gray')
plt.show()
This is a result.
enter image description here
Then, if I want to label a number into an image using the position of the matrix how to do that?
Desired result. (right image)
enter image description here
Now, My process follows this. But it's still a mistake.
How to make it better?
Follow this Code
def check_bool(x, y, size_x, size_y):
for j in range(y,y + size_y):
for k in range(x,x + size_x):
try:
if (copy_edge[k][j] == 0): #255 W 0 B
return False # ถ้าในกรอบเจอสีดำจะ set เป็น False และนำไป Plot ไม่ได้
break
except:
pass
return True
def put_text(img_plt, text, x, y):
cv2.putText(
img_plt, #numpy array on which text is written
str(text), #text
(x,y), # x y
cv2.FONT_HERSHEY_SIMPLEX, #font family
0.5, #font size
(0, 0, 0, 0), #font color
2) #font stroke
size_x = 20
size_y = 20
copy_edge = edges.copy()
color_edge = img_kmean.copy()
y, x, d = color_edge.shape
for round in range(len(mean_color)):
for y_ in range(0, y, size_y):
for x_ in range(0, x, size_x):
status = check_bool(x_, y_, size_x, size_y) # ถ้า Plot ได้
# print(status)
if status == True: # Putting text
c = sum(color_edge[y_][x_])
c0 = sum(mean_color[round])
if c == c0:
put_text(copy_edge, round, x_, y_)
plt.figure(figsize = (17,10))
plt.imshow(copy_edge,cmap = 'gray')
plt.axis('off')
To label the pixels which are not 0 you can use opencv's cv2connectedComponentsWithStats() function
import numpy as np
import cv2
from skimage.color import label2rgb
# read the image as gray channel
I = cv2.imread("imgPath", 0)
# apply canny
edges = cv2.Canny(img,100,200)
# Invert the canny image
edges = 255 - edges
#dilating the mask to merge some edges(You can skip this step)
#edges = cv2.dilate(edges, np.ones((2,2)))
# label the bw mask from canny (white pixels are labelled automatically)
n, labels, _, _ = cv2.connectedComponentsWithStats(edges)
# convert the labels to RGB for visualization purpose
labels_rgb = np.uint8(255*label2rgb(labels, bg_label=0))
#save it
cv2.imwrite("./Pictures/bw.png", labels_rgb)
Related
I try to use cv2.kmeans to segment the left auricle DICOM image as mask.
I use the following code to do the k-means binary clustering in OpenCV.
import numpy as np
import cv2
import os
from matplotlib import pyplot as plt
img = cv2.imread('1_LA.jpg')
img2 = img.reshape((-1, 3))
img2 = np.float32(img2)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret, label, center = cv2.kmeans(img2, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
cv2.imwrite('1_LA_kmeans.jpg', res2)
Then, I can get this segmentation result well.
But how can I extract one of the segmentations as mask?
I have referred other similar questions, and I try to use the code from here.
import numpy as np
import cv2
img = cv2.imread('1_LA.jpg')
Z = np.float32(img.reshape((-1,3)))
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 2
_,labels,centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
labels = labels.reshape((img.shape[:-1]))
reduced = np.uint8(centers)[labels]
result = [np.hstack([img, reduced])]
for i, c in enumerate(centers):
mask = cv2.inRange(labels, i, i)
mask = np.dstack([mask]*3) # Make it 3 channel
ex_img = cv2.bitwise_and(img, mask)
ex_reduced = cv2.bitwise_and(reduced, mask)
result.append(np.hstack([ex_img, ex_reduced]))
cv2.imwrite('kmeans/' + str(i) + '_1.jpg', np.vstack(result))
cv2.imwrite('1_LA_kmeans.jpg', np.vstack(result))
Then, I can get this output.
Becase I want to calculate the area of the left auricle, I need to extract the mask like below.
So, how can I extract one of the binary segmentation results?
Thanks for #fmw42's help.
After I refer this answer, and use the following code.
import cv2
import numpy as np
# read input and convert to range 0-1
image = cv2.imread('1.jpg')
h, w, c = image.shape
# reshape to 1D array
image_2d = image.reshape(h*w, c).astype(np.float32)
# set number of colors
numcolors = 2
numiters = 10
epsilon = 1
attempts = 10
# do kmeans processing
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, numiters, epsilon)
ret, labels, centers = cv2.kmeans(image_2d, numcolors, None, criteria, attempts, cv2.KMEANS_RANDOM_CENTERS)
# reconstitute 2D image of results
centers = np.uint8(centers)
newimage = centers[labels.flatten()]
newimage = newimage.reshape(image.shape)
#cv2.imwrite("1_test.jpg", newimage)
#cv2.imshow('new image', newimage)
#cv2.waitKey(0)
k = 0
for center in centers:
# select color and create mask
#print(center)
layer = newimage.copy()
mask = cv2.inRange(layer, center, center)
# apply mask to layer
layer[mask == 0] = [0,0,0]
#cv2.imshow('layer', layer)
#cv2.waitKey(0)
# save kmeans clustered image and layer
if(k == 0):
cv2.imwrite("1_test{0}.jpg".format(k), layer)
k = k + 1
I can extract the mask I want, appreciate.
I have some specific images of two objects (a phone and a TV remote) and I want to calculate the angle between two edges that intersect of these. I used Canny to detect the edges and Hough line for the angle, but the hough_line() function found too many angles that doesnt match the requirement.
Original image:
This is the requirement:
And this is which I made:
My code:
import cv2
from skimage.transform import hough_line, hough_line_peaks
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
def edge_detection(img, blur_ksize=5, threshold1=100, threshold2=200):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_gaussian = cv2.GaussianBlur(gray, (blur_ksize, blur_ksize), 0)
img_canny = cv2.Canny(img_gaussian, threshold1, threshold2)
return img_canny
image = edge_detection(cv2.imread('img1.png'))
h, theta, d = hough_line(image)
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
ax = axes.ravel()
ax[0].imshow(image)
ax[0].set_title('Input image')
ax[0].set_axis_off()
ax[1].imshow(image, cmap=cm.gray)
for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
y0 = (dist - 0 * np.cos(angle)) / np.sin(angle)
y1 = (dist - image.shape[1] * np.cos(angle)) / np.sin(angle)
ax[1].plot((0, image.shape[1]), (y0, y1), '-r')
ax[1].set_xlim((0, image.shape[1]))
ax[1].set_ylim((image.shape[0], 0))
ax[1].set_axis_off()
ax[1].set_title('Detected lines')
plt.tight_layout()
plt.show()
angle = []
dist = []
for _, a , d in zip(*hough_line_peaks(h, theta, d)):
angle.append(a)
dist.append(d)
angle = [a*180/np.pi for a in angle]
print(angle)
Are there any ways to detect and calculate exactly one angle I need in opencv? Thanks a lot
Update
I tried different values of blur_ksize, threshold1 and threshold2 in Canny detection, it's seem like I could remove redundant lines, but now the angles those hough_line_peaks() return are negative. Can anyone explain this for me? And I also want to put the angle values to the peaks in plot, to see which angle has which value
here is a sample solution, but I don't know whether it works for all images. You have to tune the hough transform parameters.
import cv2
import numpy as np
import matplotlib.pyplot as plt
def edge_detection(img, blur_ksize=5, threshold1=70, threshold2=200):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gaussian = cv2.GaussianBlur(gray, (blur_ksize, blur_ksize), 0)
img_canny = cv2.Canny(img_gaussian, threshold1, threshold2)
return img_canny
img = cv2.imread('stack.png')
image = edge_detection(img)
minLineLength = 300
maxLineGap = 80
lines = cv2.HoughLinesP(image,1,np.pi/180,50,minLineLength,maxLineGap)
equations = []
for line in lines:
x1,y1,x2,y2 = line[0]
equations.append(np.cross([x1,y1,1],[x2,y2,1]))
cv2.line(img,(x1,y1),(x2,y2),(255,0,0),2)
font = cv2.FONT_HERSHEY_SIMPLEX
thetas = []
N = len(equations)
for ii in range(1,N):
a1,b1,c1 = equations[0]
a2,b2,c2 = equations[ii]
# intersection point
pt = np.cross([a1,b1,c1],[a2,b2,c2])
pt = np.int16(pt/pt[-1])
# angle between two lines
num = a1*b2 - b1*a2
den = a1*a2 + b1*b2
if den != 0:
theta = abs(np.arctan(num/den))*180/3.1416
# show angle and intersection point
cv2.circle(img, (pt[0],pt[1]), 5, (255,0,0), -1)
cv2.putText(img, str(round(theta, 1)), (pt[0]-20,pt[1]-20), font, 0.8, (255,0,0), 2, 0)
thetas.append(theta)
plt.imshow(img)
plt.show()
I have a grayscale numpy image (shape=(1024, 1024, 1), dtype=float) that I'm trying to translate into the same image, but with the grayscale values assigned to the red channel (ie. the same image but in redscale).
Here's the original image:
Which is generated using numpy:
def create_mandelbrot_matrix(width, height, max_iter=100):
X = np.linspace(-2, 1, width)
Y = np.linspace(-1, 1, height)
#broadcast X to a square array
C = X[:, None] + 1J * Y
#initial value is always zero
Z = np.zeros_like(C)
exit_times = max_iter * np.ones(C.shape, np.int32)
mask = exit_times > 0
for k in range(max_iter):
Z[mask] = Z[mask] * Z[mask] + C[mask]
mask, old_mask = abs(Z) < 2, mask
#use XOR to detect the area which has changed
exit_times[mask ^ old_mask] = k
return exit_times.T
def mandelbrot_image(width, height, max_iter=100):
mandelbrot_matrix = create_mandelbrot_matrix(width, height, max_iter)
img = np.expand_dims(mandelbrot_matrix, axis=2)
return img
This function results in a totally different image from the original:
def mandelbrot_red_image(w, h):
mandelbrot_img = mandelbrot_image(w, h)
print(mandelbrot_img.shape) # (1024, 1024, 1)
img = np.zeros((w, h, 3))
img[:, :, 0] = mandelbrot_img_int.reshape((w, h))
return img
I dont know how your mandelbrot_image works, but image shapes are usually (h, w), due to the number of lines in a matrix being the first dimension, and the height.
Another point is that, maybe your dtype is not 'uint8', I had to do a conversion in order to the image appear properly.
This code worked for me
from cv2 import cv2
import numpy as np
img = cv2.imread('./mandelbrot.png', cv2.IMREAD_GRAYSCALE)
h, w = img.shape
color_img = np.zeros([h, w, 3])
color_img[:, :, 2] = img # In opencv images are BGR
cv2.imshow('color_mandelbrot', color_img.astype('uint8'))
cv2.waitKey(0)
cv2.destroyAllWindows()
This is the first time I am trying out YOLov3. I am trying to obtain the cropped image within the bounding box predicted by YOLOv3. I am able to obtain the cropped image. The problem is, the image is of a different colour than the original cropped image. I want to pass this cropped image into another model so that I can confirm that the cropped image does have fire in it.
I can't do that when the cropped image is different from the original. Any help would be appreciated.
Here is the piece of code for cropping the image:
import cv2
import numpy as np
import glob
import random
from scipy import ndimage
import matplotlib.pyplot as plt
from PIL import Image
# Load Yolo
net = cv2.dnn.readNet("D:\obj_detection\yolo_custom_detection\yolov3_training_last.weights", "D:\obj_detection\yolo_custom_detection\yolov3_testing.cfg")
# Name custom object
classes = ["fire"]
# Images path
images_path = glob.glob(r"D:\obj_detection\test_img\*.jpg")
layer_names = net.getLayerNames() #Get the name of all layers of the network.
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] #Get the index of the output layers.
#These two functions are used for getting the output layers (82,94,106).
colors = np.random.uniform(0, 255, size=(len(classes), 3))
# Insert here the path of your images
random.shuffle(images_path)
# loop through all the images
for img_path in images_path:
# Loading image
img = cv2.imread(img_path)
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape
# Detecting objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
# Syntax: blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size, mean, swapRB=True)
net.setInput(blob)
outs = net.forward(output_layers)
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.3:
# Object detected
print(class_id)
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) #Non max suppression
print(indexes)
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
if x<0:
x = 0
if y<0:
y = 0
if w<0:
w = 0
if h<0:
h = 0
print("The box dimensions:",boxes[i])
print(img_path)
label = str(classes[class_ids[i]])
color = colors[class_ids[i]]
print("Dimensions of x:", x,",y:",y,",x+w:", x+w, ",y+h:", y+h)
cropped_image = img[y:y+h, x:x+w]
plt.imshow(cropped_image)
cv2.rectangle(img, (x, y), (x + w, y + h), color, 1)
cv2.putText(img, label, (x, y + 30), font, 0.8, color, 2)
img = cv2.resize(img, (700, 600))
cv2.imshow("Fire Detection", img)
cv2.resizeWindow("Fire Detection", 1600,1200)
key = cv2.waitKey(0)
if key == ord('q'):
break
cv2.destroyAllWindows()
The output I obtain is:
Original Image:
enter image description here
Cropped image:
enter image description here
How can I obtain the cropped image as it is in the original image (the same colour)?
I have been trying to develop a program - written with python and using OpenCV -which counts the overall value of coins shown in the stream video in the real time. I should note that I am new to the opencv platform.
So, for now i have this code and it's working very well but with images, my question is how to change the input from image to a real time video stream.
Here is the code:
# import classifier
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
import math
import numpy as np
import argparse
import glob
import cv2
# construct argument parser and parse arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
# resize image while retaining aspect ratio
d = 1024 / image.shape[1]
dim = (1024, int(image.shape[0] * d))
image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
# create a copy of the image to display results
output = image.copy()
# convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# improve contrast accounting for differences in lighting conditions:
# create a CLAHE object to apply contrast limiting adaptive histogram equalization
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
gray = clahe.apply(gray)
def calcHistogram(img):
# create mask
m = np.zeros(img.shape[:2], dtype="uint8")
(w, h) = (int(img.shape[1] / 2), int(img.shape[0] / 2))
cv2.circle(m, (w, h), 60, 255, -1)
# calcHist expects a list of images, color channels, mask, bins, ranges
h = cv2.calcHist([img], [0, 1, 2], m, [8, 8, 8], [0, 256, 0, 256, 0, 256])
# return normalized "flattened" histogram
return cv2.normalize(h, h).flatten()
def calcHistFromFile(file):
img = cv2.imread(file)
return calcHistogram(img)
# define Enum class
class Enum(tuple): __getattr__ = tuple.index
# Enumerate material types for use in classifier
Material = Enum(('Copper', 'Brass', 'Euro1', 'Euro2'))
# locate sample image files
sample_images_copper = glob.glob("sample_images/copper/*")
sample_images_brass = glob.glob("sample_images/brass/*")
sample_images_euro1 = glob.glob("sample_images/euro1/*")
sample_images_euro2 = glob.glob("sample_images/euro2/*")
# define training data and labels
X = []
y = []
# compute and store training data and labels
for i in sample_images_copper:
X.append(calcHistFromFile(i))
y.append(Material.Copper)
for i in sample_images_brass:
X.append(calcHistFromFile(i))
y.append(Material.Brass)
for i in sample_images_euro1:
X.append(calcHistFromFile(i))
y.append(Material.Euro1)
for i in sample_images_euro2:
X.append(calcHistFromFile(i))
y.append(Material.Euro2)
# instantiate classifier
# Multi-layer Perceptron
# score: 0.974137931034
clf = MLPClassifier(solver="lbfgs")
# split samples into training and test data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=.2)
# train and score classifier
clf.fit(X_train, y_train)
score = int(clf.score(X_test, y_test) * 100)
print("Classifier mean accuracy: ", score)
# blur the image using Gaussian blurring, where pixels closer to the center
# contribute more "weight" to the average, first argument is the source image,
# second argument is kernel size, third one is sigma (0 for autodetect)
# we use a 7x7 kernel and let OpenCV detect sigma
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
# circles: A vector that stores x, y, r for each detected circle.
# src_gray: Input image (grayscale)
# CV_HOUGH_GRADIENT: Defines the detection method.
# dp = 2.2: The inverse ratio of resolution
# min_dist = 100: Minimum distance between detected centers
# param_1 = 200: Upper threshold for the internal Canny edge detector
# param_2 = 100*: Threshold for center detection.
# min_radius = 50: Minimum radius to be detected.
# max_radius = 120: Maximum radius to be detected.
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=2.2, minDist=100,
param1=200, param2=100, minRadius=50, maxRadius=120)
def predictMaterial(roi):
# calculate feature vector for region of interest
hist = calcHistogram(roi)
# predict material type
s = clf.predict([hist])
# return predicted material type
return Material[int(s)]
# todo: refactor
diameter = []
materials = []
coordinates = []
count = 0
if circles is not None:
# append radius to list of diameters (we don't bother to multiply by 2)
for (x, y, r) in circles[0, :]:
diameter.append(r)
# convert coordinates and radii to integers
circles = np.round(circles[0, :]).astype("int")
# loop over coordinates and radii of the circles
for (x, y, d) in circles:
count += 1
# add coordinates to list
coordinates.append((x, y))
# extract region of interest
roi = image[y - d:y + d, x - d:x + d]
# try recognition of material type and add result to list
material = predictMaterial(roi)
materials.append(material)
# write masked coin to file
if False:
m = np.zeros(roi.shape[:2], dtype="uint8")
w = int(roi.shape[1] / 2)
h = int(roi.shape[0] / 2)
cv2.circle(m, (w, h), d, (255), -1)
maskedCoin = cv2.bitwise_and(roi, roi, mask=m)
cv2.imwrite("extracted/01coin{}.png".format(count), maskedCoin)
# draw contour and results in the output image
cv2.circle(output, (x, y), d, (0, 255, 0), 2)
cv2.putText(output, material,
(x - 40, y), cv2.FONT_HERSHEY_PLAIN,
1.5, (0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
# get biggest diameter
biggest = max(diameter)
i = diameter.index(biggest)
# scale everything according to maximum diameter
# todo: this should be chosen by the user
if materials[i] == "Euro2":
diameter = [x / biggest * 25.75 for x in diameter]
scaledTo = "Scaled to 2 Euro"
elif materials[i] == "Brass":
diameter = [x / biggest * 24.25 for x in diameter]
scaledTo = "Scaled to 50 Cent"
elif materials[i] == "Euro1":
diameter = [x / biggest * 23.25 for x in diameter]
scaledTo = "Scaled to 1 Euro"
elif materials[i] == "Copper":
diameter = [x / biggest * 21.25 for x in diameter]
scaledTo = "Scaled to 5 Cent"
else:
scaledTo = "unable to scale.."
i = 0
total = 0
while i < len(diameter):
d = diameter[i]
m = materials[i]
(x, y) = coordinates[i]
t = "Unknown"
# compare to known diameters with some margin for error
if math.isclose(d, 25.75, abs_tol=1.25) and m == "Euro2":
t = "2 Euro"
total += 200
elif math.isclose(d, 23.25, abs_tol=2.5) and m == "Euro1":
t = "1 Euro"
total += 100
elif math.isclose(d, 19.75, abs_tol=1.25) and m == "Brass":
t = "10 Cent"
total += 10
elif math.isclose(d, 22.25, abs_tol=1.0) and m == "Brass":
t = "20 Cent"
total += 20
elif math.isclose(d, 24.25, abs_tol=2.5) and m == "Brass":
t = "50 Cent"
total += 50
elif math.isclose(d, 16.25, abs_tol=1.25) and m == "Copper":
t = "1 Cent"
total += 1
elif math.isclose(d, 18.75, abs_tol=1.25) and m == "Copper":
t = "2 Cent"
total += 2
elif math.isclose(d, 21.25, abs_tol=2.5) and m == "Copper":
t = "5 Cent"
total += 5
# write result on output image
cv2.putText(output, t,
(x - 40, y + 22), cv2.FONT_HERSHEY_PLAIN,
1.5, (255, 255, 255), thickness=2, lineType=cv2.LINE_AA)
i += 1
# resize output image while retaining aspect ratio
d = 768 / output.shape[1]
dim = (768, int(output.shape[0] * d))
image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
output = cv2.resize(output, dim, interpolation=cv2.INTER_AREA)
# write summary on output image
cv2.putText(output, scaledTo,
(5, output.shape[0] - 40), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 255), lineType=cv2.LINE_AA)
cv2.putText(output, "Coins detected: {}, EUR {:2}".format(count, total / 100),
(5, output.shape[0] - 24), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 255), lineType=cv2.LINE_AA)
cv2.putText(output, "Classifier mean accuracy: {}%".format(score),
(5, output.shape[0] - 8), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 255), lineType=cv2.LINE_AA)
# show output and wait for key to terminate program
cv2.imshow("Output", np.hstack([image, output]))
cv2.waitKey(0)
Video stream is just a sequence of images. Here is an example of OpenCV:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# <Put your code for processing 1 image here>
# Display the resulting frame
cv2.imshow('frame',processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
PS. You shouldn't post all your code, it's hard for others to follow. Try to make a minimal, reproducible example.