def detect_motion(frame): # make frame steams into image arrays ref = cv2.imdecode(np.fromstring(frame, dtype=np.uint8), 1) new = cv2.imdecode(np.fromstring(cls.frame, dtype=np.uint8), 1) ref_gray = cv2.cvtColor(ref.copy(), cv2.COLOR_BGR2GRAY) new_gray = cv2.cvtColor(new.copy(), cv2.COLOR_BGR2GRAY) # blur images ref_blur = cv2.GaussianBlur(ref_gray, (7, 7), 0) new_blur = cv2.GaussianBlur(new_gray, (7, 7), 0) # difference images and find change countours delta = cv2.absdiff(ref_blur, new_blur) thresh = cv2.dilate(cv2.threshold(delta, 25, 255, cv2.THRESH_BINARY)[1], None, iterations=2) (cnts, _) = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # draw bounding boxes about large contours cls.has_motion = False for c in cnts: if cv2.contourArea(c) < 500: continue cls.has_motion = True (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(new, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(new, "occupied", (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # reencode image to memory buffer new_buff = cv2.imencode(".jpeg", new, [int(cv2.IMWRITE_JPEG_QUALITY), 100])[1].tostring() try: return new_buff except NameError: return cls.frame
def run(self): # Capture first frame to get size face_cascade = cv.CascadeClassifier('cars.xml') stream = urllib.urlopen('http://213.221.150.136/mjpg/video.mjpg') # Define the codec and create VideoWriter object fourcc = cv.cv.CV_FOURCC(*'XVID') out = cv.VideoWriter('road-16-09-2015.avi', fourcc, 10.0, (640,480)) # uncomment this to write video count = 0 height = 0 width = 0 bytes = '' frame = None while count <= 1: bytes += stream.read(16384) a = bytes.find('\xff\xd8') b = bytes.find('\xff\xd9') if a != -1 and b != -1: jpg = bytes[a:b+2] bytes = bytes[b+2:] frame = cv.imdecode(np.fromstring(jpg, dtype=np.uint8), cv.CV_LOAD_IMAGE_COLOR) height, width, channels = frame.shape count += 1 print height, width cursurface = 0 #Hold the current surface that have changed grey_image = cv.cv.CreateImage([width, height], cv.cv.IPL_DEPTH_8U, 1) moving_average = cv.cv.CreateImage([width, height], cv.cv.IPL_DEPTH_32F, 3) difference = None # draw net 75% of frame size second_line = int(width/2) first_line = 10 third_line = width # define crossing time list marks_crossing_list = [0,0,0] bytes='' counter = 0 while True: bytes += stream.read(16384) a = bytes.find('\xff\xd8') b = bytes.find('\xff\xd9') if a != -1 and b != -1: jpg = bytes[a:b+2] bytes = bytes[b+2:] frame = cv.imdecode(np.fromstring(jpg, dtype=np.uint8), cv.CV_LOAD_IMAGE_COLOR) if frame is not None: # cv.imwrite(str(count) + 'road.jpg', frame) # to save images uncomment this # write the flipped frame out.write(frame) # uncomment this to write video # print(count) count += 1 cv.imshow('Target', np.asarray(frame[:,:])) if cv.cv.WaitKey(1) & 0xFF == ord('q'): break out.release() cv.destroyAllWindows()
def get_image(data): image = None if 'url' in data: url = data['url'] if url.startswith('http'): resp = urllib.urlopen(url) image = np.asarray(bytearray(resp.read()), dtype="uint8") image = cv2.imdecode(image, cv2.IMREAD_COLOR) else: image = cv2.imread(url, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image_resize(image) elif 'data' in data: _bin = data['data'] if _bin is not None: if not isinstance(_bin, list): _bin = base64.b64decode(_bin) _bin = np.fromstring(_bin, np.uint8) image = cv2.imdecode(_bin, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image_resize(image) else: image = [] for __bin in _bin: __bin = base64.b64decode(__bin) __bin = np.fromstring(__bin, np.uint8) _image = cv2.imdecode(__bin, cv2.IMREAD_COLOR) _image = cv2.cvtColor(_image, cv2.COLOR_BGR2RGB) _image = image_resize(_image) image.append(_image) return image
def update_frame(self,master,mainVideo,threshVideo): data1 = None data2 = None if self.socket_is_ready: #receiving video data by server #videoData = self.client_socket.recv(4096000) length = self.recvall(self.client_socket,16) stringData = self.recvall( self.client_socket, int(length)) data_list = stringData.split("separatore") data1 = numpy.fromstring(data_list[0], dtype='uint8') data2 = numpy.fromstring(data_list[1], dtype='uint8') image1 = cv2.imdecode(data1, 1) image2 = cv2.imdecode(data2, 1) image1 = cv2.cvtColor(image1,cv2.COLOR_BGR2RGB) #changing color space because PIL use RGB representation a1 = Image.fromarray(numpy.uint8(image1)) a2 = Image.fromarray(numpy.uint8(image2)) b1 = ImageTk.PhotoImage(image=a1) b2 = ImageTk.PhotoImage(image=a2) mainVideo.configure(image=b1) threshVideo.configure(image=b2) master.update() self.client_socket.send("soglie;"+str(self.minH)+";"+str(self.minS)+";"+str(self.minV)+";"+str(self.maxH)+";"+str(self.maxS)+";"+str(self.maxV)) #self.client_socket.send("continue") master.after(0,func=lambda:self.update_frame(master,self.main_Video,self.thresh_Video))
def do_uploadc(): print "Got it" # lines = request.files.get('lines') # colors = request.files.get('colors') line_data = request.forms.get("lines") line_data = re.sub('^data:image/.+;base64,', '', line_data) line_s = base64.b64decode(line_data) line_img = np.fromstring(line_s, dtype=np.uint8) line_img = cv2.imdecode(line_img, -1) color_data = request.forms.get("colors") color_data = re.sub('^data:image/.+;base64,', '', color_data) color_s = base64.b64decode(color_data) color_img = np.fromstring(color_s, dtype=np.uint8) color_img = cv2.imdecode(color_img, -1) # for c in range(0,3): color_img = color_img * (line_img[:,:] / 255.0) lines_img = np.array(cv2.resize(line_img, (512,512))) # lines_img = cv2.adaptiveThreshold(lines_img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2) lines_img = np.array([lines_img]) / 255.0 lines_img = lines_img[:,:,:,0] lines_img = np.expand_dims(lines_img, 3) colors_img = np.array(cv2.resize(color_img, (512,512))) # colors_img = cv2.blur(colors_img, (100, 100)) colors_img = imageblur(colors_img, True) colors_img = np.array([colors_img]) / 255.0 colors_img = colors_img[:,:,:,0:3] generated = c.sess.run(c.generated_images, feed_dict={c.line_images: lines_img, c.color_images: colors_img}) cnt = cv2.imencode(".png",generated[0]*255)[1] return base64.b64encode(cnt)
def download_data(): suffix = '_1000000' result = download_all_images() img_folder = 'test_cluster{}'.format(suffix) if not os.path.exists(img_folder): os.makedirs(img_folder) result_file = 'test_cluster{}.txt'.format(suffix) f_result = open(result_file, 'w') f_result.write('id' + '\t' + 'feature' + '\n') error_num = 0 for element in result: try: id, img, half, feature = element face_array = cv2.imdecode(np.fromstring(base64.decodestring(img), dtype=np.uint8), 1) if face_array.shape[0] < 80 or face_array.shape[1] < 80: continue half_array = cv2.imdecode(np.fromstring(base64.decodestring(half), dtype=np.uint8), 1) cv2.imwrite(os.path.join(img_folder, str(id) + '_half.jpg'), half_array) cv2.imwrite(os.path.join(img_folder, str(id) + '_face.jpg'), face_array) # 特征使用方法 # msgpack_numpy.loads(base64.b64decode(feature)) f_result.write(str(id) + '\t' + feature + '\n') except: traceback.print_exc() error_num += 1 continue print len(result), error_num
def do_uploado(): lines = request.files.get('lines') colors = request.files.get('colors') lines_img = cv2.imdecode(np.fromstring(lines.file.read(), np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED) lines_img = np.array(cv2.resize(lines_img, (512,512))) lines_img = cv2.adaptiveThreshold(cv2.cvtColor(lines_img, cv2.COLOR_BGR2GRAY), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2) lines_img = np.array([lines_img]) / 255.0 lines_img = np.expand_dims(lines_img, 3) colors_img = cv2.imdecode(np.fromstring(colors.file.read(), np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED) colors_img = np.array(cv2.resize(colors_img, (512,512))) colors_img = cv2.blur(colors_img, (100, 100)) colors_img = np.array([colors_img]) / 255.0 cv2.imwrite("uploaded/lines.jpg", lines_img[0]*255) cv2.imwrite("uploaded/colors.jpg", colors_img[0]*255) generated = c.sess.run(c.generated_images, feed_dict={c.line_images: lines_img, c.color_images: colors_img}) cv2.imwrite("uploaded/gen.jpg", generated[0]*255) return static_file("uploaded/gen.jpg", root=".", mimetype='image/jpg')
def find(image1, image2): rect = (0, 0, 0, 0) firstFrame = image1.decode('base64', 'strict') firstFrame = cv2.imdecode(np.fromstring(firstFrame, dtype=np.uint8), -1) img = image2.decode('base64', 'strict') img = cv2.imdecode(np.fromstring(img, dtype=np.uint8), -1) if firstFrame is not None and img is not None: firstGray = cv2.cvtColor(firstFrame, cv2.COLOR_BGR2GRAY) firstGray = cv2.equalizeHist(firstGray) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) frameDelta = cv2.absdiff(firstGray, gray) thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1] thresh = cv2.dilate(thresh, None, iterations=2) if platform.system() == 'Windows': _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) else: cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contourMax = None areaMax = None for c in cnts: contour = cv2.contourArea(c) if contour < 500: continue if contourMax is None or contour > contourMax: contourMax = contour areaMax = c if not areaMax is None: (x, y, w, h) = cv2.boundingRect(areaMax) rect = (x, y, w, h) return rect
def post(self): item1 = self.request.files["im1"][0] item2 = self.request.files["im2"][0] im1 = item1["body"] im2 = item2["body"] http = urllib3.PoolManager() box1 = http.request('POST', params.detect, fields={'im': (item1['filename'], im1)}) box2 = http.request('POST', params.detect, fields={'im': (item2['filename'], im2)}) img1 = cv2.imdecode(np.fromstring(im1, np.uint8), cv2.IMREAD_COLOR) img2 = cv2.imdecode(np.fromstring(im2, np.uint8), cv2.IMREAD_COLOR) box2=eval(box2.data) box1=eval(box1.data) m=[] #去除文本框置信率小于0.94的实例 for i,x in enumerate(box2): if x[4]<0.94: m.append(i) m.sort(reverse=True) for x in m: box2.remove(box2[x]) if len(box2)==0: result=[100] else: print("==============") result=distanceResult(img1,img2,box1,box2) # cls_dets = detect_im(net, im) self.set_header("Pragma", "no-cache") self.set_header("Expires", "0") self.set_header("Cache-Control", "no-cache") self.set_header("Cache-Control", "no-store") self.write(str(result))
def automateCalibration(): #Get intial distance to paper distToPaper = getCurDist(); print ("Current distance is %02d" % distToPaper); while (distToPaper > 5): #Take images of paper print ("Taking imagery... "); images = laserOnOffPictures(); img1 = cv2.imdecode(images[0], 1); img2 = cv2.imdecode(images[1], 1); cv2.imwrite('%02d_withLaser.png' % distToPaper, img1); cv2.imwrite('%02d_withOut.png' % distToPaper, img2); #Advance the robot 5 feet print ("Advancing robots position.."); ser.write('mf0005'); #Get the points of paper print ("Getting coordinates of paper... "); pts = getPaperPoints(); #Center laser/camera centerLaser(pts); #Get new distance to paper distToPaper = getCurDist(); print("Current distance is %02d" % distToPaper); return;
def get(self, segment, index, color = True, mean_sub = False): assert not self.create_mode_, 'get should not be called in create mode.' if index >= len(self.segments_[segment]): index = index % len(self.segments_[segment]) start = self.segments_[segment][index][1] size = self.segments_[segment][index][2] data = np.fromstring(self.mm_[start:(start+size)], dtype=np.uint8) if color == True: img = cv2.imdecode(data, cv.CV_LOAD_IMAGE_COLOR).astype(np.float32) if mean_sub: img -= self.mean_color_ elif color == False: img = cv2.imdecode(data, cv.CV_LOAD_IMAGE_GRAYSCALE).astype(np.float32) img = img[:,:,np.newaxis] if mean_sub: img -= self.mean_grey_ elif color == 'ycrcb': img = cv2.imdecode(data, cv.CV_LOAD_IMAGE_COLOR) img = cv2.cvtColor(img, cv.CV_BGR2YCrCb).astype(np.float32) if mean_sub: img -= self.mean_ycrcb_ elif color == 'crcb': img = cv2.imdecode(data, cv.CV_LOAD_IMAGE_COLOR) img = cv2.cvtColor(img, cv.CV_BGR2YCrCb).astype(np.float32) if mean_sub: img -= self.mean_ycrcb_ img = img[:,:,1:] return img, self.segments_[segment][index][0], self.segments_[segment][index][3]
def detect_motion(frame, reference=None): if not reference: LOG.warning("No reference image specified!") return frame, False image = cv2.imdecode(np.fromstring(frame, dtype=np.uint8), 1) reference = cv2.imdecode(np.fromstring(reference, dtype=np.uint8), 1) grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayscale_ref = cv2.cvtColor(reference, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(grayscale, (11, 11), 0) blurred_ref = cv2.GaussianBlur(grayscale_ref, (11, 11), 0) delta = cv2.absdiff(blurred, blurred_ref) thresh = cv2.threshold(delta, 25, 255, cv2.THRESH_BINARY)[1] thresh = cv2.dilate(thresh, None, iterations=2) contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) detected = False for contour in contours: # if the contour is too small, ignore it if cv2.contourArea(contour) < 80: continue # compute the bounding box for the contour, draw it on the frame x, y, w, h = cv2.boundingRect(contour) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) detected = True _, buf = cv2.imencode('.jpg', image) blob = buf.tostring() return blob, detected
def train(imageData, asciiValue): """ Takes image data and ascii value, crops symbols and feeds them to Ocrn to train into the MAINFRAME. """ # Unpacks the data sent to us. new_symbols = [] # Get the image from the DB. # string_value = r.get('expression_image:' + expression_id) # Opens buffer that takes imageValue as input image_buffer = StringIO(imageData) # Seeks to the beginning of the buffer image_buffer.seek(0) # Uses numpy to create some kind of data array with a predetermind byte encoding type img_array = np.asarray(bytearray(image_buffer.read()), dtype=np.uint8) # Gets a CV2 image from the data array. cropper = cv2.imdecode(img_array, 0) image = cv2.imdecode(img_array, 0) # The second argument, zero, is a loading argument. # We _could_ put in "cv2.CV_LOAD_IMAGE_COLOR" to load color, by why bother? ## Do something with the image, then write back some symbols to the database, I presume... # Find contours (and hierarchy? I don't know what that is...) contours,hierarchy = cv2.findContours(image,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE) # Will hold all cropped and resized images to be sent on to # Ocrn. croppedImages = [] for i,contour in enumerate(contours): if hierarchy[0,i,2] == -1 and hierarchy[0,i,3] != -1: x,y,w,h = cv2.boundingRect(contour) symbol_identifier = r.incr('symbol_identifier_ids') box = [x,y,w,h] # Resizes to 100x100 crop = cropper[y:y+h,x:x+w] # CROP resized_crop = cv2.resize(crop, (90,90)) ## THE CROPPED AND RESIZED IS RIGHT HERE ## BUT HOW DO I GET IT INTO STRING!? SHIT. crop_pil = Image.fromarray(resized_crop) crop_pil.convert("1") padded = Image.new("1", (100,100), 1) # print crop_pil.mode # print padded.mode padded.paste(crop_pil, (5,5,95,95)) croppedImages.append(padded) # Here we will want to send the (croppedImages, asciiValue) data # Through to Ocrn. # ft.feature.generateDataSetFromRoaster((croppedImages,asciiValue)) g.add_data((croppedImages, asciiValue)) return 1
def imageToFrame( image ): if six.PY2: s = StringIO() image.SaveFile( s, wx.BITMAP_TYPE_BMP ) return cv2.imdecode( np.fromstring(s.getvalue(), dtype='B'), 1 ) else: s = io.BytesIO() image.SaveFile( s, wx.BITMAP_TYPE_BMP ) return cv2.imdecode( np.fromstring(s.getbuffer(), dtype='B'), 1 )
def callback(self, data): global video global image_time #TODO If image_time is not matching with the time in odom_msgs, both computers are out of sync image_time = data.header.stamp.secs np_arr = np.fromstring(data.data, np.uint8) cv_image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) np_arr = np.fromstring(data.data, np.uint8) cv_image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) final_img = process_image(cv_image)
def convertimagetoopencvarray(image): """ convert image from jpg to and numpy array to be able to apply the opencv methods on the image """ image_array = np.asarray(bytearray(image), dtype="uint8") image_prep = cv2.imdecode(image_array, cv2.IMREAD_GRAYSCALE) # image to show the detected artifacts on later in the main thread image_show = cv2.imdecode(image_array, cv2.IMREAD_ANYCOLOR) return image_prep, image_show
def convert(image, input_type='cv2', output_type='ros'): assert input_type in ['cv2', 'ros', 'mongo'] and output_type in ['cv2', 'ros', 'mongo'] if input_type == 'mongo': image = cv2.imdecode(np.array(image, dtype=np.uint8), 1) if input_type == 'ros': image = cv2.imdecode(np.fromstring(image, np.uint8), cv2.CV_LOAD_IMAGE_COLOR) if output_type == 'ros': return np.array(cv2.imencode('.jpg', image)[1]).tostring() if output_type == 'mongo': return np.array(cv2.imencode('.jpg', image)[1]).tolist() if output_type == 'cv2': return image
def tagged_data_to_ellipse_envelope(job_spec): tag_id, remote_data_filename, remote_cal_filename, start, degrees, radius_of_roi = job_spec with open(lcu.remote_to_local_filename(remote_data_filename), 'rb') as data_file: data_jpeg = data_file.read() with open(lcu.remote_to_local_filename(remote_cal_filename), 'rb') as cal_file: cal_jpeg = cal_file.read() data = cv2.imdecode(np.fromstring(data_jpeg, np.uint8), cv2.CV_LOAD_IMAGE_GRAYSCALE) cal = cv2.imdecode(np.fromstring(cal_jpeg, np.uint8), cv2.CV_LOAD_IMAGE_GRAYSCALE) delta = lcu.better_delta(data, cal) start = np.array(start) adjust = np.array([int(radius_of_roi), int(radius_of_roi/2)], dtype=np.int32) retval, roi_corner, roi_far_corner = cv2.clipLine( (0, 0, 512, 384), tuple(start - adjust), tuple(start + adjust), ) rotate_matrix = cv2.getRotationMatrix2D(tuple(start), degrees, 1) roi = cv2.warpAffine(delta, rotate_matrix, (512, 384))[ roi_corner[1]:roi_far_corner[1], roi_corner[0]:roi_far_corner[0] ] roi = cv2.cvtColor(roi, cv2.COLOR_GRAY2RGB) roi_corner = np.array(roi_corner) start = start - roi_corner color = int(np.average( roi[start[1] - 2:start[1] + 2, start[0] - 2:start[0] + 2].astype(np.float32) )) scores = list() for x in range(20, 60): y_min = int(x/2.3) y_max = int(x/1.5) for y in range(y_min, y_max): template = np.zeros((y, x, 3), dtype=np.uint8) cv2.ellipse(img=template, box=((x // 2, y // 2), (x, y), 0), color=(color, color, color), thickness=-1) match = cv2.minMaxLoc(cv2.matchTemplate(roi, template, cv2.TM_SQDIFF_NORMED)) scores.append((match[0], (x,y), match[2])) good_scores = sorted(scores)[:10] best_score, ellipse_size, ellipse_corner = sorted(good_scores, key=lambda x: -x[1][0]*x[1][1])[0] return (tag_id, ellipse_size, color)
def update_img(pair): """Decode pairs of image strings, update a video.""" im_i, im_j = pair nparr_i = np.fromstring(str(im_i), np.uint8) img_np_i = cv2.imdecode(nparr_i, 1) img_np_i = img_np_i[..., [2, 1, 0]] nparr_j = np.fromstring(str(im_j), np.uint8) img_np_j = cv2.imdecode(nparr_j, 1) img_np_j = img_np_j[..., [2, 1, 0]] # Optionally reshape the images to be same size. frame = np.concatenate([img_np_i, img_np_j], axis=1) im.set_data(frame) return im
def buildFromMessagesCompressed(rgb_msg): frame = FrameRGB() frame.time = rgb_msg.header.stamp np_arr = np.fromstring(rgb_msg.data, np.uint8) try: frame.rgb_image = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR) except: try: frame.rgb_image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) except: return frame frame.valid = True return frame
def outputs(): global numFrames, time stream = io.BytesIO() for i in range(controlFrames): # This returns the stream for the camera to capture to e1 = cv2.getTickCount() yield stream stream.seek(0) data = np.fromstring(stream.getvalue(), dtype=np.uint8) open_cv_image = cv2.imdecode(data, 1) if filtering: filt.image = open_cv_image f, i, c, h = filt.rgbGet(cv2.CHAIN_APPROX_SIMPLE, Constants.VIDEOS_RGB_FILTER_CONSTANTS_1) output = filt.run(filt.image) try: numFrames += 1 # queue.put(open_cv_image) except: print "FULL QUEUE" stream.seek(0) stream.truncate() e2 = cv2.getTickCount() time += (e2-e1)/cv2.getTickFrequency()
def get_captcha(captcha_str): nparr = np.fromstring(captcha_str, np.uint8) ptcha = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) captcha.shape = -1, captcha = (captcha[::3].astype(np.int) + captcha[1::3].astype(np.int) + captcha[2::3].astype(np.int)) / 3 captcha = (255 - captcha).astype(np.uint8) return captcha
def process(self, stream): data = cv2.imdecode(np.fromstring(stream.getvalue(), dtype=np.uint8), 1) (orig_h, orig_w) = data.shape[:2] new_h = orig_h / 2 new_w = orig_w / 2 new_dims = (new_w, new_h) # draw a test rectangle cv2.rectangle(data, (10, 400), (300, 300), (0, 255, 0), 1) # draw a test square data[10:20, 10:20] = (255, 0, 0) (B, G, R) = cv2.split(data) zeros = np.zeros(data.shape[:2], dtype = "uint8") Brz = self.prepare_channel(B, zeros, zeros, new_dims) Grz = self.prepare_channel(zeros, G, zeros, new_dims) Rrz = self.prepare_channel(zeros, zeros, R, new_dims) Orz = self.prepare_channel(B, G, R, new_dims) row1 = np.concatenate((Orz, Rrz), axis = 1) row2 = np.concatenate((Brz, Grz), axis = 1) data = np.concatenate((row1, row2), axis = 0) return cv2.imencode('.jpg', data)[1].tostring()
def runCap(self,text): #print text buff = pickle.loads(text) frame = cv2.imdecode(buff,0) cv2.imshow('serverSending',frame) self.sock.sendto(text, (self.MCAST_GRP, self.MCAST_PORT))
def recieve_and_save(port): global running global images s = None conn = None while running: if s is None: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('0.0.0.0', port)) s.listen(True) conn, addr = s.accept() except: s = None if conn is not None: length = recvall(conn,16) if length is None: continue stringData = recvall(conn, int(length)) data = numpy.fromstring(stringData, dtype='uint8') decimg=cv2.imdecode(data,1) images[port] = decimg if s is not None: s.close()
def main(args): output_dir = os.path.expanduser(args.output_dir) if not os.path.exists(output_dir): os.mkdir(output_dir) # Store some git revision info in a text file in the output directory src_path,_ = os.path.split(os.path.realpath(__file__)) facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv)) i = 0 for f in args.tsv_files: for line in f: fields = line.split('\t') class_dir = fields[0] img_name = fields[1] + '-' + fields[4] + '.' + args.output_format img_string = fields[6] img_dec_string = base64.b64decode(img_string) img_data = np.fromstring(img_dec_string, dtype=np.uint8) img = cv2.imdecode(img_data, cv2.cv.CV_LOAD_IMAGE_COLOR) #pylint: disable=maybe-no-member if args.size>0: img = misc.imresize(img, (args.size, args.size), interp='bilinear') full_class_dir = os.path.join(output_dir, class_dir) if not os.path.exists(full_class_dir): os.mkdir(full_class_dir) full_path = os.path.join(full_class_dir, img_name) cv2.imwrite(full_path, img) #pylint: disable=maybe-no-member print('%8d: %s' % (i, full_path)) i += 1
def __iter__(self): print('iter') init_state_names = [x[0] for x in self.init_states] for k in range(self.count): data = [] label = [] for i in range(self.batch_size): num = gen_rand() img = self.captcha.generate(num) img = np.fromstring(img.getvalue(), dtype='uint8') img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE) img = cv2.resize(img, (80, 30)) img = img.transpose(1, 0) img = img.reshape((80 * 30)) img = np.multiply(img, 1/255.0) data.append(img) label.append(get_label(num)) data_all = [mx.nd.array(data)] + self.init_state_arrays label_all = [mx.nd.array(label)] data_names = ['data'] + init_state_names label_names = ['label'] data_batch = SimpleBatch(data_names, data_all, label_names, label_all) yield data_batch
def serv(self, inp_q, res_q, continue_evt, reload_val, mdl_update_pth): while True: try: continue_evt.wait() if reload_val.value: self.classify = svm_load_model(mdl_update_pth) reload_val.value = 0 cli_name, data_buffer, mode = inp_q.get(timeout=2) bbs_filt, labels, probs = [], [], [] if mode: # 1 is strong mode bbs_filt_np, feats = data_buffer bbs_filt_np = bbs_filt_np.astype(np.int64) for bbs_np in bbs_filt_np: bbs_filt.append(dlib.rectangle(bbs_np[0], bbs_np[1], bbs_np[2], bbs_np[3])) else: nparr = np.fromstring(data_buffer, dtype=np.uint8) img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) faces, bbs_filt = self.faceDetAlign(img_np) feats = self.featureExtract(faces) if len(feats): labels, probs = self.runSVM(feats.tolist(), self.classify) res_q.put((cli_name, (bbs_filt, labels, probs, feats))) inp_q.task_done() except (Queue.Empty, KeyboardInterrupt): # print 'timeout in face' pass
def handle_frame(frame): # print '\n =============== \n' # print 'received frame ' + frame # print '\n =============== \n' if not isinstance(frame, basestring): return image_ref = 'null' try: image_ref = frame.split(',')[1] except IndexError: image_ref = "null" if image_ref != "null": image = base64.b64decode(image_ref) image = np.fromstring(image, dtype=np.uint8) image = cv2.imdecode(image, 1) cropped_image, success_flag = faceCropper.process_single_image_cv2(image, NEW_IMAGE_SIZE = (100, 100)) cropped_image = cropped_image.reshape(100, 100).astype('uint8') if success_flag: # take image face_landmarks = emotionFeatures.get_landmarks_for_single_image(cropped_image).flatten() face_landmarks = face_landmarks.reshape(1, -1) prediction = svmClassifierAPI.predict(face_landmarks) print 'You are ', labels.get(prediction[0]) else: print 'skipped frame..'
def strtomat(str): arr = np.fromstring(str, np.uint8) return cv2.imdecode(arr, cv2.IMREAD_UNCHANGED)
def hash_encode_image(image, extension): """ Encode the image, get the hash and return the hash with encoded image """ img = cv2.imencode(extension, image)[1] # pylint: disable=no-member f_hash = sha1(cv2.imdecode(img, cv2.IMREAD_UNCHANGED)).hexdigest() # pylint: disable=no-member return f_hash, img
# Initial pan-tilt angles angle_x = 90 angle_y = 90 servo_x.write(angle_x) servo_y.write(angle_y) while True: try: # Create a memory stream to avoid photos needing to be saved in a file p_stream = io.BytesIO() camera.capture(p_stream, format='jpeg', use_video_port=True, thumbnail=None) buff = numpy.fromstring(p_stream.getvalue(), dtype=numpy.uint8) image = cv2.imdecode(buff, 1) # Convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Look for faces in the image faces = face_cascade.detectMultiScale(gray, 1.5, 2) if faces is not (): # Display face detection on a led GPIO.output(FACE_DETECTION_STATUS_PIN, True) # Look for the biggest face biggest_face = get_biggest_face(faces) # Compute pan-tilt angle to adjust centring
def lasermain(self): outf = open('output2.csv', 'w') with picamera.PiCamera() as camera: # camera.resolution = (320, 240) camera.resolution = (640, 320) camera.framerate = 24 camera.start_recording('timed.h264') stream = io.BytesIO() framecount = 0 laserobject = laser.Laser() while True: framecount += 1 camera.capture(stream, format="jpeg", use_video_port=True) frame = np.fromstring(stream.getvalue(), dtype=np.uint8) stream.seek(0) self.frame = cv2.imdecode(frame, 1) cv2.imshow("images", self.frame) cv2.waitKey(1000 / 24) starttime = time.time() x, y, w, h, laserstate = self.laserposition() print "xs", x, y, w, h, laserstate outf.write("x" + str(x) + "y" + str(y) + "w" + str(w) + "h" + str(h) + "laserstate" + str(laserstate) + "\n") laserobject.add(x, y, w, h, laserstate) if self.previoustate != laserstate: self.previoustate = laserstate if laserobject.transition % TRANSITIONS == 0: laserobject.transitionscount += 1 self.actionx, self.actiony = laserobject.actioncoordinate( ) clickornot = laserobject.boundaryevaluate() if clickornot: print "within----right or left click" print "transitions counter", laserobject.transitionscount #if drag is active, even if laserobject is seen as within the boundary, it will be ignored if laserobject.transitionscount <= 3 and not self.dragging: self.leftclick = True print "left click set" if laserobject.transitionscount > 3 and not self.dragging: self.rightclick = True self.leftclick = False print "right click set" laserobject.clear() #possible drag else: print "outside----dragging" print "transitions counter", laserobject.transitionscount if laserobject.transitionscount > 3: self.dragging = True self.rightclick = False self.leftclick = False print "dragging from:", self.actionx, self.actiony laserobject.clear() if x != 0: try: currentarea = w * h if currentarea < 40: outf.write("currentarea" + str(currentarea) + "\n") laser.Laser.area.append(currentarea) diff = laserobject.difference(laser.Laser.area) outf.write("diff" + str(diff) + "\n") if len(laser.Laser.area) > 15: for i in xrange(0, 10): laser.Laser.area.pop(0) if currentarea <= 16: print "blinked at:", self.actionx, self.actiony except BaseException as error: print "error", error # as soon as the transitions stop(continual beam arrives)--do a right or left click as per the flag if self.previoustate == laserstate: print "continual beam arrived" if self.leftclick: print "left click at:", self.actionx, self.actiony self.leftclick = False if self.rightclick: print "right click at:", self.actionx, self.actiony self.rightclick = False if self.dragging: print "stop drag" self.dragging = False # print "simple mouse movements", x, y, w, h #clear the last 7 transitions laserobject.clear() #clear the count of a single 7 transitions laserobject.transitionscount = 0
# This is the template for the operator running a Decision Level Task import traceback import numpy as np import cv2 import imagezmq import json image_hub = imagezmq.ImageHub(open_port='tcp://*:5575') try: while True: # show streamed images until Ctrl-C jsonstr, jpg_buffer = image_hub.recv_jpg() jsondata = json.loads(jsonstr) image = cv2.imdecode(np.frombuffer(jpg_buffer, dtype='uint8'), -1) # cv2.imshow(jsondata['sensor_id'], image) # One window for each stream # Debug print("DLO\n", flush=True) print(jsondata, flush=True) image_hub.send_reply(b'OK') # REP reply # detect any kepresses # key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop # if key == ord("q"): # break except (KeyboardInterrupt, SystemExit): pass # Ctrl-C was pressed to end program except Exception as ex:
def base64_to_cv2(b64str): data = base64.b64decode(b64str.encode('utf8')) data = np.frombuffer(data, np.uint8) data = cv2.imdecode(data, cv2.IMREAD_COLOR) return data
def recv_image(self, ros_data): np_array = np.fromstring(ros_data.data, np.uint8) image_np = cv2.imdecode(np_array, cv2.IMREAD_COLOR) blue, green, red = image_np.T image_np = np.array([red, green, blue]).transpose() self.image = image_np
# カメラ初期化 stream = io.BytesIO() with picamera.PiCamera() as camera: # 解像度の設定 camera.resolution = (640, 480) # 撮影の準備 camera.start_preview() # 準備している間、少し待機する time.sleep(1) # 撮影して指定したファイル名で保存する camera.capture(stream, format='jpeg') # 撮影した写真を読み込む data = np.fromstring(stream.getvalue(), dtype=np.uint8) img = cv.imdecode(data, 1) # 顔検出の処理効率化のために、写真の情報量を落とす(モノクロにする) grayimg = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # 顔検出のための学習元データを読み込む face_cascade = cv.CascadeClassifier( '/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml') # 目検出のための学習元データを読み込む eye_cascade = cv.CascadeClassifier( '/usr/share/opencv/haarcascades/haarcascade_eye.xml') # 顔検出を行う facerect = face_cascade.detectMultiScale(grayimg, scaleFactor=1.1, minNeighbors=1,
import cv2 import numpy as np import urllib.request import string import time arr = string.ascii_lowercase lst = [] for char in arr: lst.append(char) app = "zback"+"zspace" lst.append("zback") lst.append("zspace") for char in lst: url = "https://raw.githubusercontent.com/Ankit404butfound/HomeworkMachine/master/Images/%s.png"%char imglink=urllib.request.urlopen(url) imgNp=np.array(bytearray(imglink.read())) img = cv2.imdecode(imgNp,-1) cv2.imwrite(r"%s.png"%char,img) print(char+".png saved") time.sleep(1)
def save_tweet(analyzer, balancer, data): if not is_delete(data): if is_retweet(data): return None # Handle exception too many values to unpack text, media, student = extract_data_retweet(data) else: text, media, student, url_ = extract_data(data) ocr_res = '' media_status = None # print(f'MEDIA: {media}') if media is not None: with urllib.request.urlopen(media) as url: image_np = np.asarray(bytearray(url.read()), dtype="uint8") image = cv2.imdecode(image_np, cv2.IMREAD_GRAYSCALE) try: ocr_res = balancer.get_ocr(media) except: print('429 RAISED') print(ocr_res) media_status = get_image_sentiment(media) or analyzer.predict( ocr_res) # true: not depressed # print('================IMAGE SENTIMENT RESULT================', '\n') # print('Depressed: ' + str(not media_status), '\n') # print('================TEXT ANALYZER RESULT================', '\n') text = text.strip(url_) text_status = analyzer.predict(text) # print('Depressed: ' + str(not text_status), '\n') if text == '' and ocr_res != '': text = ocr_res if media: overall_status = text_status or media_status else: overall_status = text_status posted_date = get_post_date_time(data) tokens = analyzer.tokenized_text s_harm = get_self_harm(tokens) if s_harm: text_status = True new_tweet = Tweet(text=text, media=media, student=student, text_status=text_status, posted_date=posted_date, media_status=media_status, overall_status=overall_status, s_harm=s_harm) new_tweet.save() student.total_number_of_tweets += 1 student.save() if not overall_status: student.number_of_n_tweets += 1 if s_harm: SelfHarmTweet(tweet=new_tweet, student=student).save() profile = Profile.objects.filter(user=student.caregiver)[0] student.suicidal = True student.number_of_sh_tweets += 1 if profile.receive_alert and profile.phone and check_self_harm( new_tweet.posted_date): send_whatsapp_alert(student_name=student.name, tweet=text, phone=profile.phone) # generate_call(student_name=student.name, tweet=text, phone=profile.phone) tweets = get_tweet_by_time(student, 7) wordcloud_wraper(tweets, student.twitter_account) student.save() print(f'total # of tweets: {student.total_number_of_tweets}') print(f'Added a new tweet from {student.twitter_account}!')
def Imagecallback(msg): global tracker, frame_counter, tick_time, tree, point_3d, rot global image1 # print(float((rospy.Time.now()-msg.header.stamp).nsecs)/1e9) # if (rospy.Time.now()-msg.header.stamp).nsecs > 1e8: # print("throw one frame") # return img = cv2.imdecode(np.fromstring(msg.data, dtype=np.uint8), cv2.IMREAD_GRAYSCALE) h, w = img.shape[:2] newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h)) dst = cv2.undistort(img, mtx, dist, None, newcameramtx) x, y, w, h = roi # img is undistorted image img = dst[y:y + h, x:x + w] #crop raw image (l, w) = img.shape img = img[int(0.1 * l):int(0.9 * l), int(0.1 * w):int(0.8 * w)] s_w = 512 s_l = 384 img = cv2.resize(img, (s_w, s_l)) image1 = img # cv2.imshow("1",img) # cv2.waitKey(1) # line_detection(image1) #comepute optical flow flow = tracker.track(img) # print(flow.shape) flow_x = flow[:, :, 0] flow_y = flow[:, :, 1] xsum = np.sum(flow_x) ysum = np.sum(flow_y) if ysum >= 0: fy = 5 * ysum / 3000000 else: fy = 2 * ysum / 3000000 # print(fy) # print(xsum) fx = 3.24 * xsum / 4500000 print(fx, fy) # mag = np.hypot(flow[:, :, 0], flow[:, :, 1]) # mag = (mag/2.5 * 255).astype('uint8') # (l,w) = mag.shape # nmag = mag[int(0.1*l):int(0.9*l),int(0.1*w):int(0.8*w)] # # # print(flow.shape) # # # print(mag.shape) # # mag = (mag > 0.2*127) * mag # # imag = cv2.applyColorMap(mag, cv2.COLORMAP_HOT) # # print(mag.max()) # global mag_max, mag_sum # mag_max = 0.5 * mag.max() + 0.5 * mag_max # msum = np.sum(mag) # mag_sum = 0.5 * msum + 0.5 * mag_sum # print(mag_sum/10000) # cv2.imshow("2",imag) arrows = put_optical_flow_arrows_on_image(image1, flow) cv2.imshow('arrows', arrows) # # if img is not None: # cv2.destroyAllWindows() # image = cv2.imread('/home/zgl/Desktop/plot1.png') # print(image.shape) # cv2.imshow('image',image) cv2.waitKey() frame_counter += 1 if rospy.Time.now() - tick_time > rospy.Duration(secs=10.0): freq = frame_counter / (rospy.Time.now() - tick_time).secs frame_counter = 0 rospy.loginfo("frame rate: " + str(freq)) # mag = np.hypot(flow[:, :, 0], flow[:, :, 1]).max() # rospy.loginfo("mag: " + str(mag)) tick_time = rospy.Time.now()
def parseXmlFiles(xml_path, image_path, xml_path_for_box_with_shadow_tmp, xml_path_for_box_with_shadow_final, xml_path_for_wrong_box): num_pic = 0 file_dict = {id: name for id, name in enumerate(os.listdir(xml_path))} len_file_dict = len(file_dict) num_pic_st_len = True while num_pic_st_len: if num_pic >= len_file_dict - 1: num_pic_st_len = False f = file_dict[num_pic] num = 0 # print(f) if not f.endswith('.xml'): # jump off non-xml files continue labels = [] xml_file = os.path.join(xml_path, f) print( "#######---------########---------A New Image for you to Check its Annotation-------#######----------" ) xml_file_for_box_with_shadow = os.path.join( xml_path_for_box_with_shadow_tmp, f) xml_file_for_box_with_shadow_final = os.path.join( xml_path_for_box_with_shadow_final, f) xml_file_for_box_wrong = os.path.join(xml_path_for_wrong_box, f) tree = ET.parse(xml_file) root = tree.getroot() if root.tag != 'annotation': raise Exception( 'pascal voc xml root element should be annotation, rather than {}' .format(root.tag)) f_xml_with_shadow = open(xml_file_for_box_with_shadow, "w") head = headstr % (f) f_xml_with_shadow.write(head) for elem in root: # root.tag = Annotation if elem.tag == "filename": pic_name = elem.text[:-4] img_name = os.path.join(image_path, pic_name + '.bmp') print("current pic path is : ", img_name) # print(img_name) # img = cv2.imread(img_name, cv2.IMREAD_COLOR) img = cv2.imdecode( np.fromfile(u'{}'.format(img_name), dtype=np.uint8), 1) if elem.tag == "object": # elem.tag = frame,object is_append = True is_end = False for subelem in elem: # subelem.tag = name,bndbox if is_append: # if list was just appended, reintialize the bndbox bndbox = dict() is_append = False if subelem.tag == "name": bndbox["name"] = subelem.text if subelem.tag == "bndbox": # option.tag = xmin,ymin,xmax,ymax for option in subelem: if option.tag == 'xmin': x1 = int(option.text) if option.tag == 'ymin': y1 = int(option.text) if option.tag == 'xmax': x2 = int(option.text) if option.tag == 'ymax': y2 = int(option.text) is_end = True if is_end: # if all location and class of current bndbox have been read, append current bndbox to list pool num += 1 f_xml_with_shadow.write( objstr % (bndbox["name"], x1, y1, x2, y2)) # labels.append(bndbox) is_end = False is_append = True draw_caption(img, (x1, y1, x2, y2), bndbox["name"] + ":" + pic_name) # print(img) # img = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_BGR2RGB) cv2.rectangle(img, (x1, y1), (x2, y2), color=(0, 255, 0), thickness=2) cv2.namedWindow("img", 1) xy = (0, 0) def on_EVENT_LBUTTONDOWN(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: # xy = "%d,%d" % (x, y) cv2.rectangle(img, (x1, y1), (x2, y2), color=(0, 255, 0), thickness=2) cv2.rectangle(img, (x1, y), (x2, y2), color=(255, 0, 0), thickness=2) f_xml_with_shadow.write( objstr % (bndbox["name"] + ' with shad', x1, y, x2, y2)) cv2.imshow('img', img) cv2.imshow('img', img) cv2.setMouseCallback("img", on_EVENT_LBUTTONDOWN) # print(xy) xy = (0, 0) cv2.imshow('img', img) cv2.waitKey(0) # display new annotated img f_xml_with_shadow.write(tailstr) f_xml_with_shadow.close() print(num_pic, "th xml: ", xml_file_for_box_with_shadow, ' is processed') cv2.imshow('Press Enter to save xml_with_shad, or space to edit: ', img) # waitkey_num = cv2.waitKeyEx() if waitkey_num == 32: n_tmp = input( "Input 'wrong' for select it as wrong annotated, 'delete' for redo, or anything else for jump back:" ) if n_tmp == 'wrong': shutil.copyfile(xml_file, xml_file_for_box_wrong) print(num_pic, "th xml: ", xml_file, ' is moved to wrong path') num_pic += 1 elif n_tmp == 'delete': print(num_pic, "th xml: ", xml_file, ' is deleted, you need to reannotated now') os.remove(xml_file_for_box_with_shadow) # num_pic = num_pic else: print('Current pic number is : ', num_pic) n_tmp = input("Enter the numbers you need to jump:") num_pic = int(n_tmp) cv2.destroyAllWindows() if waitkey_num == 13: shutil.copyfile(xml_file_for_box_with_shadow, xml_file_for_box_with_shadow_final) print(num_pic, "th xml: ", xml_file_for_box_with_shadow, ' is annotated and moved to right path') num_pic += 1 cv2.destroyAllWindows()
def create_synthtext_dataset(data_root, save_path_train, save_path_test, train_ratio=0.7, shuffle=True, n_max=None): """ Create tf records for the VGG SynthText dataset Args: data_root: the root folder for the datasets save_path_train: path to save the TF record save_path_test: path to save the TF record train_ratio: the ratio of samples to be used for training. list_name: list file name shuffle: bool, whether to shuffle examples """ # load gt.mat print('Loading gt.mat ...') gt = sio.loadmat(os.path.join(data_root, 'gt.mat')) n_samples = gt['wordBB'].shape[1] print('Start writing to %s %s' % (save_path_train, save_path_test)) writer_train = tf.python_io.TFRecordWriter(save_path_train) writer_test = tf.python_io.TFRecordWriter(save_path_test) if n_max is not None: n_samples = min(n_max, n_samples) if shuffle: indices = np.random.permutation(n_samples) else: indices = np.arange(n_samples) for i in tqdm(range(n_samples)): idx = indices[i] image_rel_path = str(gt['imnames'][0, idx][0]) image_path = os.path.join(data_root, image_rel_path) # load image jpeg data with open(image_path, 'rb') as f: image_jpeg = f.read() nparr = np.fromstring(image_jpeg, np.uint8) img = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) annot_dict = {} annot_dict['folder'] = '' annot_dict['filename'] = image_rel_path annot_dict['image'] = img annot_dict['size'] = { 'height': annot_dict['image'].shape[0], 'width': annot_dict['image'].shape[1] } # word polygons word_polygons = gt['wordBB'][0, idx] if word_polygons.ndim == 2: word_polygons = np.expand_dims(word_polygons, axis=2) word_polygons = np.transpose(word_polygons, axes=[2, 1, 0]) n_words = word_polygons.shape[0] img, word_polygons = check_polygons(img, word_polygons) if not isinstance(img, np.ndarray): continue word_polygons_flat = [float(o) for o in word_polygons.flatten()] objects = [] for i, polygon in enumerate(word_polygons): minx = max(0, np.min(polygon[:, 0])) miny = max(0, np.min(polygon[:, 1])) maxx = min(img.shape[1], np.max(polygon[:, 0])) maxy = min(img.shape[0], np.max(polygon[:, 1])) cur_obj = {} cur_obj['difficult'] = 0 cur_obj['oriented_box'] = polygon cur_obj['bndbox'] = { 'xmin': minx, 'ymin': miny, 'xmax': maxx, 'ymax': maxy } cur_obj['name'] = 'text' cur_obj['truncated'] = 0 cur_obj['pose'] = '' objects.append(cur_obj) annot_dict['object'] = objects example = dict_to_tf_example(annot_dict, data_root, LABEL_MAP, image_subdirectory='') if random.random() < train_ratio: writer_train.write(example.SerializeToString()) else: writer_test.write(example.SerializeToString())
def getFrame(socket): frame = socket.recv_string() img = base64.b64decode(frame) npimg = np.fromstring(img, dtype=np.uint8) return cv2.rotate(cv2.imdecode(npimg, 1), cv2.ROTATE_180)
s.listen(True) conn, addr = s.accept() capture = cv2.VideoCapture(0) while not rospy.is_shutdown(): ret, frame = capture.read() cv2.imwrite('abc.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 90]) #encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),90] #result, imgencode = cv2.imencode('.jpg', frame, encode_param) #bitmap = cv.CreateImageHeader((imgencode.shape[1], imgencode.shape[0]), cv.IPL_DEPTH_8U, 3) #cv.SetData(bitmap, imgencode.tostring(),imgencode.dtype.itemsize * 3 * imgencode.shape[1]) f = open('abc.jpg', "rb") rawImage = f.read() f.close() #data = numpy.array(imgencode) #stringData = bitmap.tostring() #print stringData #print type(bitmap) # conn.sendall( str(len(bitmap)).ljust(16)); conn.sendall(rawImage) sock.close() decimg = cv2.imdecode(data, 1) #cv2.imshow('CLIENT',decimg) #cv2.waitKey(0) cv2.destroyAllWindows()
def handle(self): global sensor_data global prediction stream_bytes = ' ' stop_flag = False stop_sign_active = True # stream video frames one by one try: while True: stream_bytes += self.rfile.read(1024) first = stream_bytes.find('\xff\xd8') last = stream_bytes.find('\xff\xd9') if first != -1 and last != -1: jpg = stream_bytes[first:last + 2] stream_bytes = stream_bytes[last + 2:] gray = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), 0) image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), -1) global prediction up_min_location = np.argmin( np.sum(gray[150:170, :], axis=0)) ###寻黑线 low_min_location = np.argmin( np.sum(gray[210:230, :], axis=0)) ###寻黑线 tmp = up_min_location - low_min_location ###寻黑线 # up_max_location=np.argmax(np.sum(gray[150:170, :],axis=0)) ###寻白线 # low_max_location=np.argmax(np.sum(gray[210:230, :],axis=0)) ###寻白线 # tmp = up_max_location - low_max_location ###寻白线 print("tmp=", tmp) if -40 < tmp < 40: prediction = 0 #直行 elif -220 < tmp < -40: prediction = 1 #左转 elif 40 < tmp < 220: prediction = 2 #右转 else: prediction = 5 #后退 # object detection v_param1 = self.obj_detection.detect( self.stop_cascade, gray, image) v_param2 = self.obj_detection.detect( self.light_cascade, gray, image) # distance measurement if v_param1 > 0 or v_param2 > 0: d1 = self.d_to_camera.calculate( v_param1, self.h1, 300, image) d2 = self.d_to_camera.calculate( v_param2, self.h2, 100, image) self.d_stop_sign = d1 self.d_light = d2 cv2.imshow('image', image) # stop conditions if sensor_data is not None and sensor_data < 35: print("Stop, obstacle in front") prediction = 6 elif 0 < self.d_stop_sign < 35 and stop_sign_active: print("Stop sign ahead") prediction = 6 # stop for 5 seconds if stop_flag is False: self.stop_start = cv2.getTickCount() stop_flag = True self.stop_finish = cv2.getTickCount() self.stop_time = (self.stop_finish - self.stop_start ) / cv2.getTickFrequency() print "Stop time: %.2fs" % self.stop_time # 5 seconds later, continue driving if self.stop_time > 5: print("Waited for 5 seconds") stop_flag = False stop_sign_active = False elif 0 < self.d_light < 30: #print("Traffic light ahead") if self.obj_detection.red_light: print("Red light") #self.rc_car.stop() prediction = 6 #self.gpio.handle.stop() elif self.obj_detection.green_light: print("Green light") pass elif self.obj_detection.yellow_light: print("Yellow light flashing") pass self.d_light = 30 self.obj_detection.red_light = False self.obj_detection.green_light = False self.obj_detection.yellow_light = False else: time.sleep(0.05) self.stop_start = cv2.getTickCount() self.d_stop_sign = 35 if stop_sign_active is False: self.drive_time_after_stop = ( self.stop_start - self.stop_finish) / cv2.getTickFrequency() if self.drive_time_after_stop > 5: stop_sign_active = True if cv2.waitKey(1) & 0xFF == ord('q'): prediction = 6 cv2.destroyAllWindows() finally: print "Connection closed on thread 1"
def faceDR(base64_image, scale=1): imgData = base64.b64decode(base64_image) nparr = np.fromstring(imgData, np.uint8) img = cv2.imdecode(nparr, cv2.COLOR_BGR2RGB) faceinfo = main_image(img=img, scale=scale) return faceinfo
def readb64(self,base64_str): buf = base64.b64decode(base64_str) buf = np.asarray(bytearray(buf), dtype="uint8") image = cv2.imdecode(buf, cv2.IMREAD_COLOR) return image
def find(imgPath): resp = urllib.request.urlopen(imgPath) image = np.asarray(bytearray(resp.read()), dtype="uint8") img = cv2.imdecode(image, cv2.IMREAD_COLOR) # img = cv2.imread(imgPath) imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) height, width = img.shape[:2] ret, thresh = cv2.threshold(imgray, 127, 255, 0) contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnt = contours[0] x,y,w,h = cv2.boundingRect(cnt) cropImg = img[y:y+h, x:x+w] tempCoordsX = [x] white = True for i in range(x, x+w): for j in range(y, y+h): if imgray[j][i] > 0: white = True break else: if white == True: tempCoordsX.append(i) white = False tempCoordsX.append(x+w) coordinates = [] imgSymbol = [] for k in range(len(tempCoordsX)-1): tempImg = imgray[y:y+h, tempCoordsX[k]:tempCoordsX[k+1]] tempRet, tempThresh = cv2.threshold(tempImg, 127, 255, 0) tempContours = cv2.findContours(tempThresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) tempCnt = tempContours[0] rectCoords = list(cv2.boundingRect(tempCnt)) rectCoords[0] += tempCoordsX[k] rectCoords[1] += y coordinates.append(rectCoords) tempSymbol = imgray[rectCoords[1]:rectCoords[1]+rectCoords[3], rectCoords[0]:rectCoords[0]+rectCoords[2]] # tempImg = cv2.cvtColor(tempImg, cv2.COLOR_GRAY2BGR) tempWl = abs(rectCoords[0] - (rectCoords[0] + rectCoords[2])) tempHl = abs(rectCoords[1] - (rectCoords[1] + rectCoords[3])) maxL = max(tempHl, tempWl) correctX = (maxL - tempWl) // 2 correctY = (maxL - tempHl) // 2 temp = [np.zeros(maxL, dtype=np.uint8) for i in range(maxL)] temp_arr = np.array(temp, dtype=np.uint8) for i in range(tempHl): for j in range(tempWl): temp_arr[i+correctY][j+correctX] = tempSymbol[i][j] resImg = cv2.resize(temp_arr, (28-6, 28-6)) finalResImg = np.array([[0 for i in range(28)] for j in range(28)], dtype=np.uint8) for i in range(28-6): for j in range(28-6): finalResImg[i+3][j+3] = resImg[i][j] imgSymbol.append(finalResImg) # newResImg = tempResImg.resize(tempResImg, (28, 28)) img = cv2.rectangle(img,(rectCoords[0],rectCoords[1]), (rectCoords[0]+rectCoords[2],rectCoords[1]+rectCoords[3]),(0,255,0),2) # cv2.imwrite("temp/t"+str(k)+".png", finalResImg) # cv2.imwrite("temp/temp_img2.png", cropImg) # cv2.imshow("", img) # cv2.waitKey(0) # cv2.destroyAllWindows() return imgSymbol
contours, hierarchy = cv2.findContours(im_bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(image, contours, -1, (0,255,0), 3) cv2.imwrite("cnt_", image) r = requests.get('http://192.168.1.198/mjpg/video.mjpg', auth=('root', 'root'), stream=True) print("Opening the stream...") if(r.status_code == 200): bytes = bytes() for chunk in r.iter_content(chunk_size=1024): bytes += chunk a = bytes.find(b'\xff\xd8') b = bytes.find(b'\xff\xd9') if a != -1 and b != -1: jpg = bytes[a:b+2] bytes = bytes[b+2:] i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR) img = cv2.medianBlur(i,5) imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR) circles = cv2.HoughCircles(imgg,cv2.HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=1,maxRadius=30) if circles is None: continue print (circles) #circles = np.uint16(np.around(circles)) for i in circles[0,:]: cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle cv2.imshow("preview", cimg) if cv2.waitKey(1) == 27:
def readImg(s3, bucket_name, Img, typeI="p"): content = s3.get_object(Bucket=bucket_name, Key=Img) content = content["Body"].read() content = np.fromstring(content, np.uint8) content = cv2.imdecode(content, cv2.IMREAD_COLOR) / 255.0 return content
def detect(): use_webcam = True url = input( "Enter the webcam's IPv4 address (e.g. 123.123.12.1:3000) to use it, otherwise leave it blank: " ) # Insert Ip for webcam if url == "": use_webcam = False capture = cv2.VideoCapture(0) # Define min window size to be recognized as a face minW, minH = 0.1 * capture.get(3), 0.1 * capture.get(4) else: url = "http://" + url + "/shot.jpg" minW, minH = 0.1 * 432, 0.1 * 288 #default webcam size file_path = input("Enter the file path to the character dataset: ") recognizer = cv2.face.LBPHFaceRecognizer_create() face_cascade = cv2.CascadeClassifier('lbpcascade_animeface.xml') print("\n [INFO] Training faces. This will take a few seconds. Wait...") char_face, ids, char_dic = get_char(file_path) recognizer.train(char_face, ids) recognizer.save('trainer.yml') print("\n [INFO] {0} faces trained. Exiting Program".format( len(np.unique(ids)))) recognizer.read('trainer.yml') font = cv2.FONT_HERSHEY_DUPLEX while True: if use_webcam: ip_cam = urllib.request.urlopen(url) img_arr = np.array(bytearray(ip_cam.read()), dtype=np.uint8) frame = cv2.imdecode(img_arr, -1) else: __, frame = capture.read() #Reading from computer webcam gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(int(minW), int(minH))) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) id, acc = recognizer.predict(gray[y:y + h, x:x + w]) if (acc < 100): name = char_dic[id] acc = " {0}%".format(round(100 - acc)) else: name = "unknown" acc = " {0}%".format(round(100 - acc)) cv2.putText(frame, str(name), (x + 5, y - 5), font, 1, (255, 255, 255), 1) cv2.putText(frame, str(acc), (x + 5, y + h - 5), font, 1, (0, 0, 0), 1) cv2.imshow('AnimeFaceDetect', frame) esc = cv2.waitKey(10) & 0xff if esc == 27: break print("\n [INFO] Exiting Program Now...") if not use_webcam: capture.release() cv2.destroyAllWindows()
try: while not rospy.is_shutdown(): # Read the length of the image as a 32-bit unsigned int. If the # length is zero, quit the loop image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0] if not image_len: break # Construct a stream to hold the image data and read the image # data from the connection image_stream = io.BytesIO() image_stream.write(connection.read(image_len)) # Rewind the stream, open it as an image with opencv and do some # processing on it image_stream.seek(0) image = Img.open(image_stream) data = np.fromstring(image_stream.getvalue(), dtype=np.uint8) imagedisp = cv2.imdecode(data, 1) try: image_pub.publish(bridge.cv2_to_imgmsg(imagedisp, "bgr8")) except CvBridgeError as e: print(e) # cv2.imshow("Frame",imagedisp) # cv2.waitKey(1) #imshow will not output an image if you do not use waitKey # cv2.destroyAllWindows() #cleanup windows finally: connection.close() server_socket.close()
def showImage(self): self.presentImage = cv2.imdecode(np.fromfile(self.ImagesPath[self.PresentPage - 1], dtype=np.uint8), cv2.IMREAD_COLOR) image_show = self.ProcessImage(self.annotateImage(self.presentImage.copy())) self.Window.label_Image.setPixmap(QPixmap(image_show)) self.refreshInfo() self.changeLabelState()
def main(yolo): # Definition of the parameters max_cosine_distance = 0.3 nn_budget = None nms_max_overlap = 1.0 # deep_sort model_filename = 'model_data/mars-small128.pb' encoder = gdet.create_box_encoder(model_filename, batch_size=1) metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) writeVideo_flag = True #video_capture = cv2.VideoCapture(0) if writeVideo_flag: # Define the codec and create VideoWriter object #w = int(video_capture.get(3)) #h = int(video_capture.get(4)) w = 640 h = 480 fourcc = cv2.cv.CV_FOURCC(*'MJPG') #fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('output.avi', fourcc, 15, (w, h)) list_file = open('detection.txt', 'w') frame_index = -1 fps = 0.0 while True: #ret, frame = video_capture.read() # frame shape 640*480*3 #if ret != True: # break; t1 = time.time() try: frame1 = footage_socket1.recv_string() img = base64.b64decode(frame1) npimg = np.fromstring(img, dtype=np.uint8) frame = cv2.imdecode(npimg, 1) #cv2.imshow("Stream", source) #cv2.waitKey(1) image = Image.fromarray(frame) boxs = yolo.detect_image(image) # print("box_num",len(boxs)) features = encoder(frame, boxs) # score to 1.0 here). detections = [ Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features) ] # Run non-maxima suppression. boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) indices = preprocessing.non_max_suppression( boxes, nms_max_overlap, scores) detections = [detections[i] for i in indices] # Call the tracker tracker.predict() tracker.update(detections) for track in tracker.tracks: if track.is_confirmed() and track.time_since_update > 1: continue bbox = track.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2) cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) for det in detections: bbox = det.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2) cv2.imshow('', frame) if writeVideo_flag: # save a frame out.write(frame) frame_index = frame_index + 1 list_file.write(str(frame_index) + ' ') if len(boxs) != 0: for i in range(0, len(boxs)): list_file.write( str(boxs[i][0]) + ' ' + str(boxs[i][1]) + ' ' + str(boxs[i][2]) + ' ' + str(boxs[i][3]) + ' ') list_file.write('\n') encoded, buffer = cv2.imencode('.jpg', frame) jpg_as_text = base64.b64encode(buffer) footage_socket.send(jpg_as_text) fps = (fps + (1. / (time.time() - t1))) / 2 print("fps= %f" % (fps)) # Press Q to stop! if cv2.waitKey(1) & 0xFF == ord('q'): break except KeyboardInterrupt: cv2.destroyAllWindows() break video_capture.release() #if writeVideo_flag: # out.release() # list_file.close() cv2.destroyAllWindows()
def preprocess(img): img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) img = cv2.resize(img,(100,100)) img = cv2.equalizeHist(img) img = img.reshape(1,100,100,1) img = img/255 return img ret = True while ret: img_url = urllib.request.urlopen(URL) image = np.array(bytearray(img_url.read()),np.uint8) frame = cv2.imdecode(image,-1) faces = classifier.detectMultiScale(frame,1.5,5) for x,y,w,h in faces: face = frame[y:y+h,x:x+w] cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),5) cv2.putText(frame,get_pred_label(model.predict_classes (preprocess(face))[0]), (200,200),cv2.FONT_HERSHEY_COMPLEX,1, (255,0,0),2) cv2.imshow("capture",frame) if cv2.waitKey(1)==ord('q'): break
def base64_to_cv2(b64str): import base64 data = base64.b64decode(b64str.encode('utf8')) data = np.fromstring(data, np.uint8) data = cv2.imdecode(data, cv2.IMREAD_COLOR) return data
def read_b64(text): # encoded_data = uri.split(',')[1] encoded_data = str(text) np_arr = np.fromstring(base64.b64decode(encoded_data), np.uint8) img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) return img
def process_video_from_rover(self, jpegbytes, timestamp_10msec): array_of_bytes = np.fromstring(jpegbytes, np.uint8) self.image = cv2.imdecode(array_of_bytes, flags=3) k = cv2.waitKey(5) & 0xFF return self.image
def streamingAndCollectData(self): saved_frame = 0 total_frame = 0 # collect images for training print('Start collecting images...') e1 = cv2.getTickCount() file_name = self.file_name starting_value = self.starting_value training_data = [] for i in list(range(4))[::-1]: print(i+1) time.sleep(1) last_time = time.time() paused = False print('STARTING!!!') try: print("Connection from: ", self.client_address) print("Streaming...") print("Press 'Q' to exit") stream_bytes = b'' frame = 1 while True: stream_bytes += self.connection.read(1024) first = stream_bytes.find(b'\xff\xd8') last = stream_bytes.find(b'\xff\xd9') self.conn.sendall(b'WA') if first != -1 and last != -1: jpg = stream_bytes[first:last + 2] stream_bytes = stream_bytes[last + 2:] image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_UNCHANGED) # select lower half of the image roi = image[120:240, :] last_time = time.time() # run a color convert: screen = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) cv2.imshow('image', screen) cv2.imwrite('training_images/Imageframe{:>05}.jpg'.format(frame), screen) frame += 1 total_frame += 1 # Check key stroke while streaming keys = key_check() if 'W' in keys and 'A' in keys: print("Forward Left") training_data.append([screen,self.wa]) saved_frame += 1 # Send key to rest api payload = dict(data='WA') response = requests.post(self.restUrl, data=payload) print(response, payload, 'sent to server.') elif 'W' in keys and 'D' in keys: print("Forward Right") training_data.append([screen,self.wd]) saved_frame += 1 # Send key to rest api payload = dict(data='WD') headers = { 'content-type': 'application/json' } response = requests.post(self.restUrl, data=json.dumps(payload), headers=headers ) print(response, payload, 'sent to server.') elif 'S' in keys and 'A' in keys: print("Reverse Left") training_data.append([screen,self.sa]) # Send key to rest api payload = dict(data='SA') response = requests.post(self.restUrl, data=payload) print(response, payload, 'sent to server.') elif 'S' in keys and 'D' in keys: print("Reverse Right") training_data.append([screen,self.sd]) # Send key to rest api payload = dict(data='SD') response = requests.post(self.restUrl, data=payload) print(response, payload, 'sent to server.') elif 'W' in keys: print("Forward") saved_frame += 1 training_data.append([screen,self.w]) # Send key to rest api payload = dict(data='W') response = requests.post(self.restUrl, data=payload) print(response, payload, 'sent to server.') elif 'S' in keys: print("Reverse") saved_frame += 1 training_data.append([screen,self.s]) # Send key to rest api payload = dict(data='S') response = requests.post(self.restUrl, data=payload) print(response, payload, 'sent to server.') elif 'D' in keys: print("Right") saved_frame += 1 training_data.append([screen,self.d]) # Send key to rest api payload = dict(data='D') response = requests.post(self.restUrl, data=payload) print(response, payload, 'sent to server.') elif 'A' in keys: print("Left") saved_frame += 1 training_data.append([screen,self.a]) # Send key to rest api payload = dict(data='A') response = requests.post(self.restUrl, data=payload) print(response, payload, 'sent to server.') elif 'Q' in keys: print('exit') training_data.append([screen,self.nk]) self.send_inst = False # Send key to rest api payload = dict(data='Q') response = requests.post(self.restUrl, data=payload) print(response, payload, 'sent to server.') break np.save(file_name,training_data) e2 = cv2.getTickCount() # calculate streaming duration time0 = (e2 - e1) / cv2.getTickFrequency() print('Streaming duration:', time0) print('Total frame:', total_frame) print('Saved frame:', saved_frame) print('Dropped frame', total_frame - saved_frame) finally: self.connection.close() self.server_socket.close()
def run(self): print "I'm here " + self.ip_address # create dgram udp socket try: self.pi_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) except socket.error: print 'Failed to create socket' return try: # Set the whole string self.pi_socket.sendto(const.CMD_CONNECT, (self.ip_address, const.PORT)) print 'send ok' while self.running: try: t1 = time.time() reply, addr = self.pi_socket.recvfrom(50000) arr = reply.split('daicahuy') dataRight = numpy.fromstring(arr[0], dtype='uint8') dataLeft = numpy.fromstring(arr[1], dtype='uint8') decimgRight = cv2.imdecode(dataRight, 1) decimgLeft = cv2.imdecode(dataLeft, 1) self.queue_process_to_frm.put(decimgRight) cv2.waitKey(1) print 'fps = ' + str(1/(time.time()-t1)) except Exception, ex: print 'Thread_Listening_Socket Exception: ' + str(ex) except Exception, ex: print 'Thread_Listening_Socket Exception: ' + str(ex)