What I’m trying to do:
I’m taking a video as a file and sending it to my google colab for prediction. Before the predicting can begin I’m converting the video into frames that I can predict on. This is where I’m getting the TypeError.
What I’ve tried and what’s not working:
It’s saying:
TypeError: an integer is required (got type StreamingMedia)
This error is occurring at the following line:
# Read the video from specified path
cam = cv2.VideoCapture(name)
Code Sample:
Here is my file loader function
def file_loader_1_change(self, file, **event_args):
"""This method is called when a new file is loaded into this FileLoader"""
result , score = anvil.server.call('pred',file)
if result == 0:
self.l1.text = "Poor shot , your shot is %0.2f% effective"%(score)
elif result == 1:
self.l1.text = "Good shot , your shot is %0.2f% effective"%(score)
else:
self.l1.text = "No shot detected"
pass
Here is my pred() function:
import cv2
import os
import numpy as np
from google.colab import files
from keras.preprocessing import image
@anvil.server.callable
def pred(name):
# Read the video from specified path
cam = cv2.VideoCapture(name)
try:
# creating a folder named data
if not os.path.exists('data'):
os.makedirs('data')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
# frame
currentframe = 0
while(True):
# reading from frame
ret,frame = cam.read()
if ret:
# if video is still left continue creating images
name = './data/frame' + str(currentframe) + '.jpg'
print ('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
# increasing counter so that it will
# show how many frames are created
currentframe += 1
else:
break
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
p = [0]*3
for i in os.listdir('/content/data/'):
path = '/content/data/' + i
img = image.load_img(path, target_size=(640, 360))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x /= 255.0
images = np.vstack([x])
t=model.predict(images,batch_size=32)
p[t.argmax()] += 1
if p[0] > 5 or p[1] > 5:
if p[0] >= p[1]:
return 0 , 60 - p[0]%60
else:
return 1 , 60 + p[1]%40
else:
return 2 , 0
Clone link:
https://anvil.works/build#clone:ENUQGLIASUO55S4J=K2VXQ6UEPODG42P33BANV2T3