def putincnn(self, tensor_track, model): # put data in cnn print("predicting...") print('-' * 50) output = predict(tensor_track, model) print(output) return output
def upload_image(): """ Returns JSON in the form: { 'class': str(cls), 'probability': float(conf), 'blurredImage': str(encoded_img), 'maskImage': str(encoded_img_mask), 'unblurredMask': str(encoded_img_saliency) } Where images are in base64. @return: JSON """ data = request.get_json() # Handle empty request if data is None: print("[ ERR ] Request body invalid: empty") return "Error: Missing Request" # Decode image and predict output img = Image.open(BytesIO(base64.urlsafe_b64decode(data['img']))) print("[ INF ] Success: Image Loaded") return jsonify(load_model.predict(img))
waveform = waveform.numpy()[0, :] length = np.shape(waveform)[0] for index in range(0, length, STEP_SIZE): waveform_part = waveform[index:index + int(FRAME_SIZE)] # [44100] if len(waveform_part) < FRAME_SIZE: break waveform_part = waveform_part[np.newaxis, ...] # [1, 44100] waveform_part = torch.from_numpy(waveform_part) # torch [1, 44100] print(np.shape(waveform_part)) mel_specgram = torchaudio.transforms.MelSpectrogram(new_sample_rate)( waveform_part) # torch [1, 128, 221] mel_specgram = mel_specgram.detach().numpy() # numpy [1, 128, 221] if np.shape(waveform_part)[1] == int(FRAME_SIZE): track_array.append(mel_specgram) tensor_track = torch.Tensor(track_array) # load model model = torch.load('../model/harmonica_model/harmonica_error_2d_model_15.pth') print("loading model...") print('-' * 50) # put data in cnn print("predicting...") print('-' * 50) output = predict(tensor_track, model) print(output)
x, y, w, h = 0, 0, 0, 0 while ret: ret, framecolor = cap.read() frame = cv2.cvtColor(framecolor, cv2.COLOR_BGR2GRAY) frame_sub = backsub.apply(frame) contours, hierarchy = cv2.findContours(frame_sub, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) for item in contours: if cv2.contourArea(item) > 350: x, y, w, h = cv2.boundingRect(item) croped = frame[y:y + h, x:x + w] resized = cv2.resize(croped, dsize=(64, 64)) img_tensor = np.reshape(resized, newshape=(1, 64, 64, 1)) # now predict k = predict(img_tensor) img = cv2.rectangle(framecolor, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=3) if k == 1: cv2.putText(img, 'human', org=(x, y), fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=1, color=(255, 0, 0), bottomLeftOrigin=False) cv2.imshow(winname='video', mat=img) if cv2.waitKey(1) & 0xFF == ord('q'): break