def visual(self, img, img_name='img', save_flag=False):
     if self.visual_flag:
         cv2.imshow(img_name, img)
         cv2.waitKey(0)
         cv2.destroyAllWindows()
         if save_flag:
             cv2.write(img_name, img)
Example #2
0
    def splitTransform(self):
        """
        将透视变换后的图像拆分
        """
        path_merge = './images/deform/de_merge'
        path_train = './images/deform/de_train/'
        path_label = './images/deform/de_label/'

        train_imgs = glob.glob(path_merge + '/*.' + self.img_type)
        for imgname in train_imgs:
            midname = imgname[imgname.rindex("/") + 1 : imgname.rindex("." + self.img_type)]
            img = cv2.imread(imgname)
            img_train = img[:,:,2]
            img_label = img[:,:,0]
            cv2.write(path_train + midname + '.' + self.img_type, img_train)
            cv2.write(path_label + midname + '.' + self.img_type, img_label)
def screenRecorder():
    fourcc = cv2.VideoWritter_fourcc(*'XVID')
    out = cv2.VideoWritter("output.avi", fourcc, (1366, 768))

    while True:
        img = ImageGrab.grab()
        img_np = np.array(img)
        frame = cv2.cvtcolor(img_np, cv2.COLOR_BGRGB)
        cv2.imshow("Screen Recorder", frame)
        cv2.write(frame)

        if cv2.waitKey(1) == 27:
            break

    out.release()
    cv2.destroyAllWindow()
Example #4
0
def Test():
    print("For test.\n")
    transform = torchvision.transforms.Compose([
        torchvision.transforms.RandomRotation(30),
        torchvision.transforms.RandomHorizontalFlip(),
        torchvision.transforms.RandomResizedCrop(112, scale=(0.8, 1.2)),
        torchvision.transforms.ToTensor()
    ])
    dataset = MyDataSet(transform=transform, root="/home/diqong/output/")
    #dataset = MyDataSet(transform=transform, root="./output/")
    trainloader = torch.utils.data.DataLoader(dataset,
                                              batch_size=1,
                                              shuffle=True,
                                              num_workers=32)
    inputdata, target = dataset.__getitem__(2)
    img = inputdata.data.cpu().numpy()
    cv2.write("1.jpg", img)
Example #5
0
def getPrintscreen():
    while(1):
        img = ImageGrab.grab()
        imgPath = 'pictures\\'
        img.save(imgPath + 'printscreen.jpg')
        print("print screen saved")
        rawImg = cv2.imread(imgPath + 'printscreen.jpg')
        print("print screen loaded")
        imgGrey = cv2.cvtColor(rawImg, cv2.COLOR_BGR2GRAY)
        print("print screen converted to grey scale")
#        imgGrey = cv2q.GaussianBlur(imgGrey, (3, 3), 0)
#        print("print screen blurred")
#        sift = cv2.SIFT()
        sift = cv2.xfeatures2s.SIFT_create()
        kp = sift.detect(imgGrey, None)
        imgSift = cv2. drawKeypoins(imgGrey, kp)
        cv2.write(imgPath + 'siftKeypoints.jpg', imgSift)

        # detect edges in the image
        imgEdged = cv2.Canny(imgGrey, 180, 200)
        print("Image edges detected")

        # image close operation
        Imgkernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
        Imgclosed = cv2.morphologyEx(imgEdged, cv2.MORPH_CLOSE, Imgkernel)
        print("Image close operation")

        circles = cv2.HoughCircles(imgEdged,cv2.HOUGH_GRADIENT,1,10,
                            param1=90,param2=90,minRadius=0,maxRadius=150)

        circles = np.uint16(np.around(circles))
        print("Hough transformation performed")

        for i in circles[0,:]:
            print(i)
            # draw the outer circle
            cv2.circle(rawImg,(i[0],i[1]),i[2],(0,255,0),2)
            # draw the center of the circle
            cv2.circle(rawImg,(i[0],i[1]),2,(0,0,255),3)

        print("save modified jpg")
        cv2.imwrite(imgPath + 'rawImg.jpg', rawImg)
        cv2.imwrite(imgPath + 'imgEdged.jpg', Imgclosed)
#        cv2.imshow("Edged", edged)
#        cv2.waitKey(0)
        time.sleep(300)
def detect_faces(image_path):
    image = cv2.imread(image_path, 0)
    #image_grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    faces = FACE_CASCADE.detectMultiScale(image,
                                          scaleFactor=1.16,
                                          minNeighbors=5,
                                          minSize=(25, 25),
                                          flags=0)
    for x, y, w, h in faces:
        sub_image = image[y - 10:y + h + 10, x - 10:x + w + 10]
        os.chdir("Extracted")
        cv2.write(str(randint(0, 10000)) + ".jpg", sub_image)
        os.chdir("../")
        cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)

    cv2.imshow("Faces Found", image)
    if (cv2.waitKey(0) & 0xFF == ord('q')) or (cv2.waitKey(0) & 0xFF
                                               == ord('Q')):
        cv2.destroyAllWindows()
    def find_pose(self, img, draw=True, save_img_path=None):
        """
        Parameters
        ----------
        img : image variable
            Image on which the pose has to be estimated.
        draw : bool, optional
            Whether to draw the pose on the image/video. The default is True.
        save_img_path : bool, optional
            Whether to save the image. Works only when self.mode = True. 
            To save the image assign the path to the save_img_path variable. The default is None.

        Returns
        -------
        img : list
            The input image
        """

        if self.mode:
            save_img_path = None

        img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
        self.results = self.pose.process(img)

        if self.results.pose_landmarks:
            if draw:
                if not self.mode:
                    img = cv.cvtColor(img, cv.COLOR_RGB2BGR)

                self.mp_draw.draw_landmarks(img, self.results.pose_landmarks,
                                            self.mp_pose.POSE_CONNECTIONS)

            if save_img_path:
                cv.write(save_img_path + ".png", img)

        else:
            print("Pose detection failed. No objects/images/poses found")

        return img
def infer_on_stream(args, client):
    """
    Initialize the inference network, stream video to network,
    and output stats and video.

    :param args: Command line arguments parsed by `build_argparser()`
    :param client: MQTT client
    :return: None
    """
    args=build_argparser().parse_args()
    single_image_mode=False
    
    # Initialise the class
    infer_network = Network()
 
    # Set Probability threshold for detections
    prob_threshold = args.prob_threshold
    ###Load the model through `infer_network` ###
    num_requests=2
    infer_network.load_model(args.model, args.device,args.cpu_extension)
    input_shape=infer_network.get_input_shape()
    n,c,h,w=input_shape
       
    ### TODO: Handle the input stream ###
        # Checks for live feed
    if args.input == 'CAM':
        args.input= 0

    # Checks for input image
    elif args.input.endswith('.jpg') or args.input.endswith('.bmp') :
        single_image_mode = True

    # Capture video
    capture=cv2.VideoCapture(args.input)
    capture.open(args.input)
    
    if single_image_mode:
        out=None
    else:
        out=cv2.VideoWriter('output.mp4',0x00000021,30,(100,100))
        
    if not capture.isOpened():
        log.error("Input not supported")
    
    width_=capture.get(3)
    height_=capture.get(4)
    
    
    #Init Variables
    fcounter=0
    etime=0
    c_count=0
    pcount=0
    tcount=0
    count_list=deque(maxlen=FRAME_KEEP)
    
    #Loop until stream is over
    while capture.isOpened():
        flag,frame=capture.read()
        if not flag:
            break
        
        key_pressed=cv2.waitKey(60)
        
        #Pre-processing the input/frame
        
        proc_frame=cv2.resize(frame,(w,h))
        proc_frame=proc_frame.transpose((2,0,1))
        proc_frame=proc_frame.reshape(1, *proc_frame.shape)
        
        #Async inference
        infer_network.init_async_infer(proc_frame)
        start=time.time()
        fcounter=fcounter+1
        
        #Waiting for result
        if infer_network.wait()==0:
            end=time.time()
            time_difference=end-start
            
            #Result from the inference
            result=infer_network.get_output()
            
            #Extract the desired stats from the result
            frame,c_counter=draw_box(frame,result,args.prob_threshold,width_,height_)
                #Calculate and send relevant information on current_count, total_count and duration to the MQTT server #
                ### Topic "person": keys of "count" and "total" ###
                ### Topic "person": keys of "count" and "total" ###
            message="Time: {:.3f}ms".format(time_difference*1000)
            cv2.putText(frame,message,(15,15),cv2.FONT_HERSHEY_COMPLEX,0.5,(10,200,10),1)
            count_list.append(c_count)
            average_count=sum(count_list)/4
            keep_count=int(np.ceil(average_count))
                
            if fcounter%FRAME_KEEP==0:
                if keep_count>pcount:
                    etime=time.time()
                    tcount+=(keep_count-pcount)
                    client.publish("person",json.dumps({"total":tcount}))
                if keep_count<pcount:
                    duration=int(time.time()-etime)
                    client.publish("person/duration",json.dumps({"duration":duration}))
                client.publish("person",json.dumps({"count":keep_count}))
                pcount=keep_count
            if key_pressed==27:
                break
        if not single_image_mode:
            sys.stdout.buffer.write(frame)
            sys.stdout.flush()
                
        if single_image_mode:
            cv2.write('output_img.jpg',frame)
        capture.release()
        cv2.destroyAllWindows()
        client.disconnect()
def create_dataset(img, id, img_id):
    cv2.write("data/pic." + str(id) + "." + str(img_id) + ".jpg", img)
Example #10
0
def main(args=None):
	parser = argparse.ArgumentParser(description='Simple training script for training a RetinaNet network.')

	parser.add_argument('--dataset', help='Dataset type, must be one of csv or coco.')
	parser.add_argument('--coco_path', help='Path to COCO directory')
	parser.add_argument('--csv_classes', help='Path to file containing class list (see readme)')
	parser.add_argument('--csv_val', help='Path to file containing validation annotations (optional, see readme)')
	parser.add_argument('--img_path', help='Path to file to save val images')

	parser.add_argument('--model', help='Path to model (.pt) file.')

	parser = parser.parse_args(args)

	if parser.dataset == 'coco':
		dataset_val = CocoDataset(parser.coco_path, set_name='train2017', transform=transforms.Compose([Normalizer(), Resizer()]))
	elif parser.dataset == 'csv':
		dataset_val = CSVDataset(train_file=parser.csv_train, class_list=parser.csv_classes, transform=transforms.Compose([Normalizer(), Resizer()]))
	else:
		raise ValueError('Dataset type not understood (must be csv or coco), exiting.')

	sampler_val = AspectRatioBasedSampler(dataset_val, batch_size=1, drop_last=False)
	dataloader_val = DataLoader(dataset_val, num_workers=1, collate_fn=collater, batch_sampler=sampler_val)

	retinanet = torch.load(parser.model)

	use_gpu = True

	if use_gpu:
		if torch.cuda.is_available():
			retinanet = retinanet.cuda()

	if torch.cuda.is_available():
		retinanet = torch.nn.DataParallel(retinanet).cuda()
	else:
		retinanet = torch.nn.DataParallel(retinanet)

	retinanet.eval()

	unnormalize = UnNormalizer()

	def draw_caption(image, box, caption):

		b = np.array(box).astype(int)
		cv2.putText(image, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 2)
		cv2.putText(image, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)

	for idx, data in enumerate(dataloader_val):

		with torch.no_grad():
			st = time.time()
			if torch.cuda.is_available():
				scores, classification, transformed_anchors = retinanet(data['img'].cuda().float())
			else:
				scores, classification, transformed_anchors = retinanet(data['img'].float())
			print('Elapsed time: {}'.format(time.time()-st))
			idxs = np.where(scores.cpu()>0.5)
			img = np.array(255 * unnormalize(data['img'][0, :, :, :])).copy()

			img[img<0] = 0
			img[img>255] = 255

			img = np.transpose(img, (1, 2, 0))

			img = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_BGR2RGB)

			for j in range(idxs[0].shape[0]):
				bbox = transformed_anchors[idxs[0][j], :]
				x1 = int(bbox[0])
				y1 = int(bbox[1])
				x2 = int(bbox[2])
				y2 = int(bbox[3])
				label_name = dataset_val.labels[int(classification[idxs[0][j]])]
				draw_caption(img, (x1, y1, x2, y2), label_name)

				cv2.rectangle(img, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
				print(label_name)

			cv2.write(img_path + str(idx) + ".jpeg", img)
			cv2.waitKey(0)
Example #11
0
import numpy as np
import pandas as pd
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('/home/ai3/Desktop/common/ML/Day12/walking.png', 0)

ret, thresh1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)

titles = ['OriginalImg', 'BINARY']
images = [img, thresh1]

imgx = np.hstack([thresh1])

cv.imshow("desd", imgx)
cv.waitKey(0)

cv.write("walking binary.jpg")
import cv2

img=cv2.imread('imagem.png',cv2.IMREAD_UNCHANGED)

print("Dimensao Original: " img.shape)

scale_percent=20
largura = int (img.shape[1]* scale_percent/100)
altura = int (img.shape[0]* scale_percent/100)
dim=(largura,altura)

resized= cv2.resized(img, dim, interpolationcv2.INTER_AREA)

print("Dimensao Obtida: ", resized.shape)

cv2.write('escala.png', resized)
parser.add_argument('--gpu',
                    dest="gpu",
                    type=int,
                    default=-1,
                    help='enter the number of gpu')

parser.add_argument('--image_out',
                    dest="out",
                    default="out.jpg",
                    help='enter the path to the output')

args = parser.parse_args()
files = glob("Images/normal/*")
from Inference import Infer

Inference_class = Infer(detect_thresh=0.5, gpu=args.gpu)
for n, i in enumerate(files):
    i = cv2.imread(i)
    bboxes, classes, scores = Inference_class.infer(image_path=i,
                                                    out="out/Classification/" +
                                                    str(n) + ".jpg")

    for s, b in enumerate(bboxes):
        y, x, y2, x2 = b
        cv2.putText(l, classes[s], (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255), 2, cv2.LINE_AA)
        cv2.rectangle(l, (int(x), int(y)), (int(x2), int(y2)), (0, 255, 0), 2)

    cv2.write("out/Classification/" + str(n) + ".jpg", i)
Example #14
0
        if retval:
            #first, make sure that the image exists!
            if None in [dim == 0 for dim in img.shape].any():
                raise (Exception(
                    "snapped an empty image, you should restart the camera"))
            else:
                return img
        else:
            return False


if __name__ == "__main__":
    cs = cameraService(timeout=5)
    img = cs.snap("front")
    if img:
        cv2.write("front_img.png")
        print("snapped a new front image, check it out!")
    else:
        print("failed to snap a front image :(")

#-------------------------- REFERENCE JUNK BELOW -------------------------------
quit()
while True:
    for i in [-1, -2]:
        cameras[i] = cv2.VideoCapture(i)
        print("camera", i, "is opened:", cameras[i].isOpened())
    if all([camera.isOpened() for camera in cameras]):
        break

firstTime = time.time()
lastTime = time.time()
# from dataloaders import dataloader_rgbd
# import numpy as np
# import cv2
# import os

# dataset_path = '/tmp/Projects2021/rgbd_dataset/nyu_data/'
# dtloader = nyu2_dataloader(dataset_path, 8, image_size=[256, 128])
# X_test, y_test = dtloader.get_testing_sample()

# dataset_path2 = '/tmp/Projects2021/depth_estimation/final-project-monodepth-ccny/extrascripts/depth_pred/'
# depth_images = os.listdir(dataset_path2)
# depth_images.sort()
# depth_images = [str(dataset_path2) + file for file in depth_images]
# image_main = np.array([])
# for i, depth_file in enumerate(depth_images):
#     depth_img = cv2.imread(depth_file)
#     depth_orig = y_test[i]
#     depth_img = np.concatenate((depth_img, depth_orig), axis=1)
#     image_main = np.append(image_main, depth_img)
#     if i > 8:
#         break

cv2.write('res_figure1.png', image_main)

dataset_path = '/tmp/Projects2021/rgbd_dataset/indoor'
dtloader = dataloader_rgbdfft(dataset_path, 8, image_size=[256, 256, 1])

X, y = dtloader.__getitem__(0)
print(X.shape)
print(y.shape)
Example #16
0
def infer_on_stream(args, client):
    """
    Initialize the inference network, stream video to network,
    and output stats and video.

    :param args: Command line arguments parsed by `build_argparser()`
    :param client: MQTT client
    :return: None
    """
    
    num_requests = 0
    
    # Flag for the single image
    single_image_mode = False

    
    # Initialise the class
    infer_network = Network()
    
    # Set Probability threshold for detections
    prob_threshold = args.prob_threshold
    
    ### TODO: Load the model through `infer_network` ###
    n, c, h, w = infer_network.load_model(args.model, args.device, 1, 1, num_requests, args.cpu_extension)[1]

    ### TODO: Handle the input stream ###
    
    # Check for Webcam
    if args.input =="CAM":
        input_stream = 0
        
    # Check for Image (jpg, bmp, png)
    elif args.input.endswith(".jpg") or args.input.endswith(".bmp") or args.input.endswith(".png") :
        single_image_mode = True
        input_stream = args.input
        
    # Check for video    
    else:
        input_stream = args.input
        assert os.path.isfile(args.input), "There is no video file"

    ### TODO: Loop until stream is over ###
    cap = cv2.VideoCapture(input_stream)
    
    if input_stream:
        cap.open(args.input)
    if not cap.isOpened():
        log.error("Error: No video source")
    
    ### Variables
    total_count = 0
    duration = 0
    person_on_screen = False
    person_count = 0
    no_person_count = 0
    people_count = 0
    duration_time = 0
    width = cap.get(3)
    height = cap.get(4)
    durration_flag = 0
    new_person_flag = 0
    person_leaves_flag = 0
    request_id = 0
    i_start = 0
    person_detected = 0

        ### TODO: Read from the video capture ###
    while cap.isOpened():
        flag, frame = cap.read()
        if not flag:
            break
        key_pressed = cv2.waitKey(60)

        ### TODO: Pre-process the image as needed ###
        image = cv2.resize(frame, (w, h))
        # HWC to CHW
        image = image.transpose((2, 0, 1))
        image = image.reshape((n, c, h, w))

        ### TODO: Start asynchronous inference for specified request ###
        infer_network.exec_net(image,request_id)
        inf_start_time = time.time()

        ### TODO: Wait for the result ###
        if infer_network.wait(request_id) == 0:
            det_time = time.time() - inf_start_time
            current_count = 0
            
            ### TODO: Get the results of the inference request ###
            result = infer_network.get_output(request_id)
            
            ##Put the inference time in the frame ###
            time_message = "Inference time: {:.3f}ms".format(det_time * 1000)
            mes_on_frame_01 = "Model: " + args.model
            mes_on_frame_02 = "Threshold: " + str(prob_threshold)
            cv2.putText(frame,time_message, (15, 15),cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1)
            cv2.putText(frame,mes_on_frame_01, (15, 30),cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1)
            cv2.putText(frame,mes_on_frame_02, (15, 45),cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1)    
            
            for obj in result[0][0]:
                if obj[2] > prob_threshold:
                    current_count = current_count + 1
                    xmin = int(obj[3] * width)
                    xmin = int(obj[3] * width)
                    ymin = int(obj[4] * height)
                    xmax = int(obj[5] * width)
                    ymax = int(obj[6] * height)
                    cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 55, 255),1)
                    person_detected = 1
            
            if person_detected:
                person_count = person_count + 1
                no_person_count = 0
            
            else:
                no_person_count = no_person_count +1
                person_count = 0
                
            person_detected = 0
            
            if person_count == 5 and person_on_screen == False:
                person_on_screen = True
                current_count = 1
                i_start = time.time()
                person_count = 0
                no_person_count = 0
                durration_flag = 0
            
            elif no_person_count == 5 and person_on_screen == True:
                person_on_screen = False
                current_count = 0
                i_start = time.time() - i_start
                person_count = 0
                no_person_count = 0
                total_count = total_count +1
                duration_time = duration_time + i_start
                i_start = 0
                duration = round(duration_time / total_count)
                durration_flag = 1
        
            client.publish("person", json.dumps({"count":current_count}))
            if durration_flag:
                client.publish("person/duration", json.dumps({"duration": duration}))
                durration_flag = 0

        ### TODO: Send the frame to the FFMPEG server ###
        sys.stdout.buffer.write(frame)
        sys.stdout.flush()

        ### TODO: Write an output image if `single_image_mode` ###
        if single_image_mode:
            cv2.write("out.jpg", frame)
            
    cap.release()
    cv2.destroyAllWindows()
    client.disconnect()
    infer_network.clean()
Example #17
0

def rescale_frame(frame, percent=75):
    width = int(frame.shape[1] * percent/ 100)
    height = int(frame.shape[0] * percent/ 100)
    dim = (width, height)
    return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)


while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    #frame = rescale_frame(frame, percent=150)
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectmultiScale(gray,scaleFactor=1.5, minNeighbour=5)
    for(x, y, w, h) in faces:
    print(x,y,w,h)
    roi_gray = gray[y:y+h, x:x+w]
    img_item ="my-image.png"
    cv2.write(img_item, roi_gray)
   
    # Display the resulting frame
    cv2.imshow('frame',frame)
    #cv2.imshow('gray',gray)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
    if len(sys.argv) > 2:
        im = sys.argv[1]
    else:
        print("Please pass a image file as argument")
        exit(1);

    image = cv2.imread(im)

    # Predict result with network
    result = network.predict(format_image(image))


SAVE_DIR = os.path.join("..","output");

# Write results to output folder
if result is not None:
     for index, emotion in enumerate(EMOTIONS):

           # Appends a descriptive text of the detected image
           cv2.putText(image, emotion, (10, index * 20 + 20), cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 255, 0), 1);

           # Append a rectangle area against the detect image
           cv2.rectangle(image, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1)

     #Appends the emotion
     face_image = feelings_faces[result[0].index(max(result[0]))]

     cv2.write(os.path.join(SAVE_DIR,"app_output.jpg"),face_image)

     
Example #19
0
def infer_on_stream(args, client):
    """
    Initialize the inference network, stream video to network,
    and output stats and video.
    :param args: Command line arguments parsed by `build_argparser()`
    :param client: MQTT client
    :return: None
    """
    args = build_argparser().parse_args()
    single_image_mode = False

    # Initialise the class
    infer_network = Network()
    model = args.model
    video_file = args.input
    extnsn = args.cpu_extension
    device = args.device

    start_time = 0
    cur_request_id = 0
    last_count = 0
    total_count = 0

    n, c, h, w = infer_network.load_model(model, device, 1, 1, cur_request_id,
                                          extnsn)[1]

    ### TODO: Handle the input stream ###
    # Checks for live feed
    if video_file == 'CAM':
        input_stream = 0

    # Checks for input image
    elif video_file.endswith('.jpg') or video_file.endswith('.bmp'):
        single_image_mode = True
        input_stream = video_file

    else:
        input_stream = video_file
        assert os.path.isfile(video_file), "File doesn't exist"

    try:
        # Capture video
        capture = cv2.VideoCapture(video_file)
    except FileNotFoundError:
        print("Cannot locate the file: " + video_file)
    except Exception as e:
        print("Something went wrong with the file: " + e)

    global initial_w, initial_h, prob_threshold
    total_count = 0
    duration = 0
    initial_w = capture.get(3)
    initial_h = capture.get(4)
    # Set Probability threshold for detections
    prob_threshold = args.prob_threshold
    temp = 0
    tk = 0
    #Loop until stream is over
    while capture.isOpened():
        flag, frame = capture.read()
        if not flag:
            break

        key_pressed = cv2.waitKey(60)

        #Pre-processing the input/frame
        image = cv2.resize(frame, (w, h))
        image = image.transpose((2, 0, 1))
        image.reshape((n, c, h, w))

        #Async inference
        inf_start = time.time()
        infer_network.exec_net(cur_request_id, image)
        color = (255, 0, 0)

        #Waiting for result
        if infer_network.wait(cur_request_id) == 0:
            time_elapsed = time.time() - inf_start

            #Result from the inference
            result = infer_network.get_output(cur_request_id)

            #Bounting box
            frame, current_count, d, tk = draw_box(result, frame, initial_w,
                                                   initial_h, temp, tk)

            #inference time
            inf_timemsg = "Inference Time: {:,3f}ms".format(time_elapsed *
                                                            1000)
            cv2.putText(frame, inf_timemsg, (15, 15), cv2.FONT_HERSHEY_COMPLEX,
                        0.5, color, 1)

            #Calculating and sending info
            if current_count > last_count:
                start_time = time.time()
                total_count = total_count + current_count - last_count
                client.publish("person", json.dumps({"total": total_count}))

            if current_count < last_count:
                duration = int(time.time() - start_time)
                client.publish("person/duration",
                               json.dumps({"duration": duration}))

            text_2 = "Distance: %d" % d + " Lost frame: %d" % tk
            cv2.putText(frame, text_2, (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.5,
                        color, 1)

            text_2 = "Current count: %d" % current_count
            cv2.putText(frame, text_2, (15, 45), cv2.FONT_HERSHEY_COMPLEX, 0.5,
                        color, 1)

            if current_count > 3:
                text_2 = "Maximum count reached!!!"
                (text_width,
                 text_height) = cv2.getTextSize(text_2,
                                                cv2.FONT_HERSHEY_COMPLEX,
                                                0.5,
                                                thickness=1)[0]
                text_offset_x = 10
                text_offset_y = frame.shape[0] - 10
                box_coords = ((text_offset_x, text_offset_y + 2),
                              (text_offset_x + text_width,
                               text_offset_y - text_height - 2))
                cv2.rectangle(frame, box_coords[0], box_coords[1], (0, 0, 0),
                              cv2.FILLED)
                cv2.putText(frame, text_2, (text_offset_x, text_offset_y),
                            cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1)

            client.publish("person", json.dumps({"count": current_count}))

            last_count = current_count
            temp = d
            if key_pressed == 27:
                break

        sys.stdout.buffer.write(frame)
        sys.stdout.flush()

        #Saving Image
        if single_image_mode:
            cv2.write('output_image.jpg', frame)

    capture.release()
    cv2.destroyAllWindows()
    client.disconnect()
    infer_network.clean()
Example #20
0
import math
from fish2pano_cv2_rgb import fish2pano
import time
import os
import subprocess as sp
import sys

cap = cv2.VideoCapture(0)

while (cap.isOpened()):
    ret, frame = cap.read()  #print(type(frame), frame.dtype)
    #    print(frame.shape[0], frame.shape[1])
    start = time.time()
    pano = fish2pano(frame)  #.astype('u8')
    #pano= cv2.logPolar(frame, (frame.shape[0]/2, frame.shape[1]/2), 100, cv2.WARP_FILL_OUTLIERS)      # [0]: height; [1]: width
    cv2.write('frame.png', pano)  # % count, pano)
    sp.call(
        '/usr/local/bin/ffmpeg -f image2 -s 240x960 -r 1/2 -i frame.png -vcodec mjpeg http://localhost:8090/cam2.ffm',
        shell=True)
    #sp.call('/usr/local/bin/ffmpeg -f image2 -s 240x960 -r 1/5 -i frame.png -f mpegts udp://localhost:1234', shell=True)

    # to simply display
    #cv2.imshow('preview', pano)

    end = time.time()
    print(end - start)
    #cv2.waitKey(1000)

#sp.call(['/usr/local/bin/ffmpeg -f image2 -s 240x960 -r 1 -i image2pipe -vcodec mpeg4 -y mvoei.mpeg'], shell=True)

cap.release()
Example #21
0
    imgPath = urllib.request.urlopen(url)
    imgNp = numpy.array(bytearray(imgPath.read()), dtype=numpy.uint8)
    im = cv2.imdecode(imgNp, -1)
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(im, (x, y), (x + w, y + h), (255, 255, 255), 4)
        face = gray[y:y + h, x:x + w]
        face_resize = cv2.resize(face, (width, height))
        prediction = model.predict(face_resize)
        cv2.rectangle(im, (x, y), (x + w, y + h), (255, 255, 255), 4)
        if prediction[1] < 800:
            cv2.putText(im,
                        '%s - %.0f' % (names[prediction[0]], prediction[1]),
                        (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN, 2,
                        (0, 255, 255))
            print(names[prediction[0]])
            cnt = 0
        else:
            cnt += 1
            cv2.putText(im, 'unknowN', (x - 10, y - 10),
                        cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255))
            print("Unknown")
            cv2.write("unknown.jpg", im)
            cnt = 0

    cv2.imshow("FaceRecongintion", im)
    if cv2.waitKey(10) == 27:
        break
cv2.destroyAllWindows()