Sunday 6 March 2022

How to convert video to gif using python opencv

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import cv2
from sklearn.metrics import mean_squared_error
from math import sqrt
import numpy as np
import traceback as tb
import images_to_gif as ig
from PIL import Image

cap = cv2.VideoCapture('..\\test\\videoplayback.mp4')
# Check if camera opened successfully
if (cap.isOpened()== False): 
  print("Error opening video  file")
farmes_list = list()
while(cap.isOpened()):
	ret, frame = cap.read()
	ret, frame = cap.read()
	if ret == True:
		# Display the resulting frame
		cv2.imshow('Frame', frame)
		farmes_list.append(frame)
	else:
		break
	# Press Q on keyboard to  exit
	if cv2.waitKey(25) & 0xFF == ord('q'):
		break

print(f'length of the frame list is= {len(farmes_list)}')
i = 0
new_frame = list()
for img in farmes_list:
	try:
		frame = img
		# Open image in bwDir - The searched image
		searchedImageBw = np.array(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
		# Open image to be compared
		inx = i
		if inx != len(farmes_list):
			cmpImage = np.array(cv2.cvtColor(farmes_list[inx+1], cv2.COLOR_BGR2GRAY))
			rms = sqrt(mean_squared_error(searchedImageBw, cmpImage))
			print(f'rms= {rms}')
			if rms>3:
				#farmes_list.remove(frame)
				new_frame.append(frame)


	except Exception as e:
		print(e)
		tb.print_exc()
		pass
	i = i+1
   

print(f'length of the frame list is= {len(new_frame)}')

pil_frame = [ Image.fromarray(img) for img in new_frame]
bytesio_object = ig.frame_gif(pil_frame)
ig.save(bytesio_object, path = "videotogif.gif")
cap.release()
cv2.destroyAllWindows()

Sunday 23 January 2022

Fire off function without waiting for answer (Python)

Here is sample code for thread based method invocation additionally desired threading.stack_size can be added to boost the performance. Also its important to invoke Garbage collector if the number of threaded invocation is greater in number.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import threading
import requests
import gc
#The stack size set by threading.stack_size is the amount of memory to allocate for the call stack in threads.
threading.stack_size(524288)

def alpha_gun(url, json, headers):
    #r=requests.post(url, data=json, headers=headers)
    r=requests.get(url)
    print(r.text)


def trigger(url, json, headers):
    threading.Thread(target=alpha_gun, args=(url, json, headers)).start()


url = "https://raw.githubusercontent.com/jyotiprakash-work/Live_Video_steaming/master/README.md"
payload="{}"
headers = {
  'Content-Type': 'application/json'
}

for i in range(10):
    print(i)
    #for condition 
    if i==5:
        trigger(url=url, json =payload, headers=headers)
        gc.collect()
        print('invoked')
    

Saturday 22 January 2022

Face Recognition With python and face-net model

 Here i have added face recognition code in a flask app. The Face app contains registration, training model and recognition where each having a separate end point.




To access the endpoints please follow cUrls-

For Respiration- 
curl -X POST \
  http://127.0.0.1:5000/upload \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -H 'postman-token: 2bf86477-f928-9ff2-d677-9b42a802e381' \
  -F file=@WIN_20220123_00_56_49_Pro.jpg \
  -F id=jp

For training-
curl -X POST \
  http://127.0.0.1:5000/train \
  -H 'cache-control: no-cache' \
  -H 'postman-token: 374826ca-15b5-7508-5052-f3ec43b1ca07'

For Recognition-
curl -X POST \
  http://127.0.0.1:5000/recognize \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -H 'postman-token: 4183a550-5278-a7fe-c618-f100d164c7f8' \
  -F file=@WIN_20220123_00_57_28_Pro.jpg