def load_tile_lowres(self, tile): '''load a lower resolution tile from cache to fill in a map while waiting for a higher resolution tile''' if tile.zoom == self.min_zoom: return None # find the equivalent lower res tile (lat,lon) = tile.coord() width2 = TILES_WIDTH height2 = TILES_HEIGHT for zoom2 in range(tile.zoom-1, self.min_zoom-1, -1): width2 /= 2 height2 /= 2 if width2 == 0 or height2 == 0: break tile_info = self.coord_to_tile(lat, lon, zoom2) # see if its in the tile cache key = tile_info.key() if key in self._tile_cache: img = self._tile_cache[key] if img == self._unavailable: continue else: path = self.tile_to_path(tile_info) try: img = cv.LoadImage(path) # add it to the tile cache self._tile_cache[key] = img while len(self._tile_cache) > self.cache_size: self._tile_cache.popitem(0) except IOError as e: continue # copy out the quadrant we want availx = min(TILES_WIDTH - tile_info.offsetx, width2) availy = min(TILES_HEIGHT - tile_info.offsety, height2) if availx != width2 or availy != height2: continue cv.SetImageROI(img, (tile_info.offsetx, tile_info.offsety, width2, height2)) img2 = cv.CreateImage((width2,height2), 8, 3) try: cv.Copy(img, img2) except Exception: continue cv.ResetImageROI(img) # and scale it scaled = cv.CreateImage((TILES_WIDTH, TILES_HEIGHT), 8, 3) cv.Resize(img2, scaled) #cv.Rectangle(scaled, (0,0), (255,255), (0,255,0), 1) return scaled return None
def crop(self, image, subRect): cv.SetImageROI(image, subRect) img2 = cv.CreateImage(cv.GetSize(image), image.depth, image.nChannels) #img3 = cv.CreateImage(cv.GetSize(image), image.depth, image.nChannels); cv.Copy(image, img2) #img3 = self.equilizer.normalize(img2) cv.ResetImageROI(image) return img2
def DetectRedEyes(image, faceCascade, eyeCascade): min_size = (20,20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 # Allocate the temporary images gray = cv.CreateImage((image.width, image.height), 8, 1) smallImage = cv.CreateImage((cv.Round(image.width / image_scale),cv.Round (image.height / image_scale)), 8 ,1) # Convert color input image to grayscale cv.CvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) face_region = cv.GetSubRect(image,(x,int(y + (h/4)),w,int(h/2))) cv.SetImageROI(image, (pt1[0], pt1[1], pt2[0] - pt1[0], int((pt2[1] - pt1[1]) * 0.7))) eyes = cv.HaarDetectObjects(image, eyeCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, (15,15)) if eyes: # For each eye found for eye in eyes: # Draw a rectangle around the eye cv.Rectangle(image, (eye[0][0], eye[0][1]), (eye[0][0] + eye[0][2], eye[0][1] + eye[0][3]), cv.RGB(255, 0, 0), 1, 8, 0) cv.ResetImageROI(image) return image
def dect_image(filename): img = cv.LoadImage(filename) gray = cv.CreateImage(cv.GetSize(img), 8, 1) cv.CvtColor(img, gray, cv.CV_BGR2GRAY) cv.EqualizeHist(gray, gray) rects = detect(img, cascade) if len(rects) != 0: rect = (rects[0][0], rects[0][1], rects[0][2] - rects[0][0], rects[0][3] - rects[0][1]) cv.SetImageROI(img, rect) cv.SaveImage(filename, img)
def area_to_image(self, lat, lon, width, height, ground_width, zoom=None, ordered=True): '''return an RGB image for an area of land, with ground_width in meters, and width/height in pixels. lat/lon is the top left corner. The zoom is automatically chosen to avoid having to grow the tiles''' img = cv.CreateImage((width, height), 8, 3) tlist = self.area_to_tile_list(lat, lon, width, height, ground_width, zoom) # order the display by distance from the middle, so the download happens # close to the middle of the image first if ordered: (midlat, midlon) = self.coord_from_area(width / 2, height / 2, lat, lon, width, ground_width) tlist.sort(key=lambda d: d.distance(midlat, midlon), reverse=True) for t in tlist: scaled_tile = self.scaled_tile(t) w = min(width - t.dstx, scaled_tile.width - t.srcx) h = min(height - t.dsty, scaled_tile.height - t.srcy) if w > 0 and h > 0: cv.SetImageROI(scaled_tile, (t.srcx, t.srcy, w, h)) cv.SetImageROI(img, (t.dstx, t.dsty, w, h)) cv.Copy(scaled_tile, img) cv.ResetImageROI(img) cv.ResetImageROI(scaled_tile) # return as an RGB image cv.CvtColor(img, img, cv.CV_BGR2RGB) return img
def find_squares4(color_img): """ Finds multiple squares in image Steps: -Use Canny edge to highlight contours, and dilation to connect the edge segments. -Threshold the result to binary edge tokens -Use cv.FindContours: returns a cv.CvSequence of cv.CvContours -Filter each candidate: use Approx poly, keep only contours with 4 vertices, enough area, and ~90deg angles. Return all squares contours in one flat list of arrays, 4 x,y points each. """ #select even sizes only width, height = (color_img.width & -2, color_img.height & -2) timg = cv.CloneImage(color_img) # make a copy of input image gray = cv.CreateImage((width, height), 8, 1) # select the maximum ROI in the image cv.SetImageROI(timg, (0, 0, width, height)) # down-scale and upscale the image to filter out the noise pyr = cv.CreateImage((width / 2, height / 2), 8, 3) cv.PyrDown(timg, pyr, 7) cv.PyrUp(pyr, timg, 7) tgray = cv.CreateImage((width, height), 8, 1) squares = [] # Find squares in every color plane of the image # Two methods, we use both: # 1. Canny to catch squares with gradient shading. Use upper threshold # from slider, set the lower to 0 (which forces edges merging). Then # dilate canny output to remove potential holes between edge segments. # 2. Binary thresholding at multiple levels N = 11 for c in [0, 1, 2]: #extract the c-th color plane cv.SetImageCOI(timg, c + 1) cv.Copy(timg, tgray, None) cv.Canny(tgray, gray, 0, 50, 5) cv.Dilate(gray, gray) squares = squares + find_squares_from_binary(gray) # Look for more squares at several threshold levels for l in range(1, N): cv.Threshold(tgray, gray, (l + 1) * 255 / N, 255, cv.CV_THRESH_BINARY) squares = squares + find_squares_from_binary(gray) return squares
def draw(self, img, pixmapper, bounds): '''draw the thumbnail on the image''' thumb = self.img() (px,py) = pixmapper(self.latlon) # find top left px -= thumb.width/2 py -= thumb.height/2 w = thumb.width h = thumb.height (px, py, sx, sy, w, h) = self.clip(px, py, w, h, img) cv.SetImageROI(thumb, (sx, sy, w, h)) cv.SetImageROI(img, (px, py, w, h)) cv.Copy(thumb, img) cv.ResetImageROI(img) cv.ResetImageROI(thumb) # remember where we placed it for clicked() self.posx = px+w/2 self.posy = py+h/2
def blend_views(bldIm, frame, mask, frec, max_rec): maskMat = cv.fromarray(mask) dispX = int(np.round(frec.left - max_rec.left)) dispY = int(np.round(frec.top - max_rec.top)) bldROI = cv.GetImage(bldIm) cv.SetImageROI(bldROI, (dispX, dispY, int(np.round( frec.width())), int(np.round(frec.height())))) cv.Add(bldROI, cv.fromarray(frame), bldROI, maskMat) cv.ResetImageROI(bldROI) cv.Copy(bldROI, bldIm) return bldIm
def calibrate(self, img, mask_range = (50, 100)): # mask: ignore pixels that are really close or far away, like the # Nao (close, low values) and kinect noise (far far away, high values) _, _, width, height = cv.GetImageROI(img) mask = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1) cv.InRangeS(img, mask_range[0], mask_range[1], mask) # using the mask, create a new image containing only the pixels of # interest. self.base_img = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1) cv.SetImageROI(self.base_img, cv.GetImageROI(img)) # get the minimum value, and subtract that from all pixels so only # the difference in depth in the image remains. minimum, _, _, _ = cv.MinMaxLoc(img, mask) cv.SubS(img, minimum, self.base_img)
def addText(self, frame, textTop, textBottom): s = cv.GetSize(frame) offset = 8 ## add space for text notations textSize = cv.GetTextSize(textTop, self._font) textframe = cv.CreateImage( (s[0], s[1] + 4*textSize[1] + 2*offset), frame.depth, frame.channels) cv.Set(textframe, 0) cv.SetImageROI(textframe, (0, 2*textSize[1] + offset, s[0], s[1])) cv.Copy(frame, textframe) cv.ResetImageROI(textframe) ## write text cv.PutText(textframe, textTop, (5, 2*textSize[1] + offset/2), self._font, self._fontcolor) cv.PutText(textframe, textBottom, (5, int(s[1] + 4*textSize[1] + 1.5 * offset)), self._font, self._fontcolor) return textframe
def detect_and_draw(img, cascade, mask): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage((cv.Round( img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if (cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.)) if faces: for ((x, y, w, h), n) in faces: # Affichage du carré de recherche xmoustache = int((x * image_scale) + w * 0.5) ymoustache = int((y * image_scale) + h * 1.25) wmoustache = int(w * 0.5 * image_scale) hmoustache = int(h * 0.19 * image_scale) img_mask = cv.CreateImage((wmoustache, hmoustache), mask.depth, mask.nChannels) cv.SetImageROI( img, (xmoustache, ymoustache, wmoustache, hmoustache)) cv.Resize(mask, img_mask, cv.CV_INTER_LINEAR) # Affichage du carré de recherche cv.Sub(img, img_mask, img) cv.ResetImageROI(img) pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) #cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) cv.ShowImage("result", img)
def save_clip_img(): img=cv.LoadImage('static/InterceptedIMG/clip.jpg') vertical_distance_decimal,vertical_distance_integer = math.modf(float(640)/19) parallel_distance_decimal,parallel_distance_integer = math.modf(float(480)/19) #print vertical_distance_decimal,vertical_distance_integer,parallel_distance_decimal,parallel_distance_integer draw_img = cv2.imread('static/InterceptedIMG/clip.jpg') for i in range(19): for j in range(19): cv2.rectangle(draw_img,(0+int(33.68*i),int(25.26*j)),(int(33.68*(i+1)),int(25.26*(j+1))),(0,255,0),1) cv2.imshow('image',draw_img) k = cv2.waitKey(0) & 0xFF if k == 27: cv2.destroyAllWindows() for i in range(19): for j in range(19): wn_position =(int(vertical_distance_integer*i)+int(vertical_distance_decimal*i),int(parallel_distance_integer*j)+int(parallel_distance_decimal*j)) es_position =(int(vertical_distance_integer*(i+1)+int(vertical_distance_decimal*i)),int(parallel_distance_integer*(j+1))+int(parallel_distance_decimal*j)) img_backup=cv.CloneImage(img) cv.SetImageROI(img_backup,(wn_position[0],wn_position[1],33,25)) cv.SaveImage('static/ClippedImg/%d_%d.jpg'%(j,i),img_backup)
#grabbing a frame, applying blur, flipping the image frame = cv.QueryFrame(camera) cv.Smooth(frame, frame, cv.CV_BLUR, 3) cv.Flip(frame, frame, 1) #drawing the rectangle and writing the text temp1 = cv.CloneImage(frame) cv.Rectangle(temp1, point1, point2, color, 1) cv.PutText(temp1, "Place in box", (430, 240), font, color) cv.PutText(temp1, "then hit q", (430, 260), font, color) #taking snapshot after q is pressed if cv.WaitKey(10) == 113: flag = 1 cv.SetImageROI(temp1, (300, 200, 100, 100)) template = cv.CloneImage(temp1) cv.ResetImageROI(temp1) cv.DestroyWindow("Image") if flag == 0: cv.NamedWindow("Image", 1) cv.MoveWindow("Image", 300, 0) cv.ShowImage("Image", temp1) continue W, H = cv.GetSize(frame) w, h = cv.GetSize(template) width = W - w + 1 height = H - h + 1
values = [cv.QueryHistValue_1D(hist, n) for n in range(255)] max_value = max(values) for n, value in enumerate(values): cv.Rectangle(canvas, (0, n), (int((value / max_value) * 200), n + 1), (255), cv.CV_FILLED) cv.SetImageROI(canvas, (0, 0, canvas.width, canvas.height)) return canvas if len(sys.argv) < 3: print "Usage: %s calibration-img.png test-img.png [test-img.png 2 ...]" exit(0) filter = TiltFilter() input_img = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_GRAYSCALE) cv.SetImageROI(input_img, (0, 0, 630, 440)) filter.calibrate(input_img) for path in sys.argv[2:]: img = cv.LoadImage(path, cv.CV_LOAD_IMAGE_GRAYSCALE) cv.ShowImage("Normal", render_with_histogram(img)) filter.filter(img) cv.ShowImage("Filtered", render_with_histogram(img)) if cv.WaitKey(0) in (-1, 27): break
name = path.split("/")[-1].split(".")[0] #printlist (image, annotations) lst.write(path + " " + div_json_path + "/" + folder + "_" + name + ".json\n") #dividepics #print json for every image out = open(div_json_path + "/" + folder + "_" + name + ".json", 'w') img = cv.LoadImage(path) count = 0 for group in anno["annotations"]: count = count + 1 _x = int(math.floor(group["x"])) _y = int(math.floor(group["y"])) _width = int(math.ceil(group["width"])) _height = int(math.ceil(group["height"])) _class = group["class"].encode() cv.SetImageROI(img,(_x, _y, _width, _height)) # print pics_path + "/" + folder + "_" + name + "_" + str(count) + "_" + _class + ".png" group.clear() group["x"] = _x group["y"] = _y group["width"] = _width group["height"] = _height group["class"] = _class string = pics_path + "/" + folder + "_" + name + "_" + str(count) + "_" + _class + ".png" group["imgname"] = string.encode() cv.SaveImage(string, img) out.write(json.dumps(anno)) #print anno
def SetImageROItoBlob(img, blob): cv.SetImageROI( img, (blob.minx, blob.miny, blob.maxx - blob.minx, blob.maxy - blob.miny))
cv.Sobel(grayIPimage,sobel,2,0,7) # 进行x方向的sobel检测 temp = cv.CreateImage(cv.GetSize(sobel),cv2.IPL_DEPTH_8U, 1) #图像格式转换回8位深度已进行下一步处理 cv.ConvertScale(sobel, temp,0.00390625, 0) cv.Threshold(temp, temp, 0, 255, cv2.THRESH_OTSU) kernal = cv.CreateStructuringElementEx(3,1, 1, 0, 0) cv.Dilate(temp, temp,kernal,2) cv.Erode(temp, temp,kernal,4) cv.Dilate(temp, temp,kernal,2) # cv.ShowImage('1', temp) kernal = cv.CreateStructuringElementEx(1,3, 0, 1, 0) cv.Erode(temp, temp,kernal,1) cv.Dilate(temp, temp,kernal,3) # cv.ShowImage('2', temp) temp = np.asarray(cv.GetMat(temp)) contours, heirs = cv2.findContours(temp,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) for tours in contours: rc = cv2.boundingRect(tours) if rc[2]/rc[3] >= 2: #rc[0] 表示图像左上角的纵坐标,rc[1] 表示图像左上角的横坐标,rc[2] 表示图像的宽度,rc[3] 表示图像的高度, cv2.rectangle(image, (rc[0],rc[1]),(rc[0]+rc[2],rc[1]+rc[3]),(255,0,255)) imageIp = cv.GetImage(cv.fromarray(image)) cv.SetImageROI(imageIp, rc) imageCopy = cv.CreateImage((rc[2], rc[3]),cv2.IPL_DEPTH_8U, 3) cv.Copy(imageIp, imageCopy) cv.ResetImageROI(imageIp) cv.SaveImage('D:/pic/result/' + str(i) + '.jpg',imageCopy) i = i +1 # cv2.imshow("黑底白字",image) cv2.waitKey(0) #暂停用于显示图片
result.append((r[0][0], r[0][1], r[0][0] + r[0][2], r[0][1] + r[0][3])) if result[0][2] > 300 and result[0][3] > 300: return result else: return [] def draw_rects(img, rects, color): if rects: for i in rects: cv.Rectangle(img, (int(rects[0][0]), int(rects[0][1])), (int(rects[0][2]), int(rects[0][3])), cv.CV_RGB(0, 255, 0), 1, 8, 0) cascade = cv.Load( "/home/flyvideo/caffe-master/examples/Face_rect/haarcascade_frontalface_alt.xml" ) if __name__ == '__main__': img = cv.LoadImage('/home/flyvideo/caffe-master/examples/Face_rect/4.jpg') gray = cv.CreateImage(cv.GetSize(img), 8, 1) cv.CvtColor(img, gray, cv.CV_BGR2GRAY) cv.EqualizeHist(gray, gray) rects = detect(img, cascade) rect = (rects[0][0], rects[0][1], rects[0][2] - rects[0][0], rects[0][3] - rects[0][1]) #draw_rects(img, rects, (0, 255, 0)) cv.SetImageROI(img, rect) cv.SaveImage('face.jpg', img)
def detectFace(self, cam_img, faceCascade, eyeCascade, mouthCascade): #cam_img should be cv2.cv.iplcam_img min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 image_width = int(cam_img.get(cv.CV_CAP_PROP_FRAME_WIDTH)) image_height = int(cam_img.get(cv.CV_CAP_PROP_FRAME_HEIGHT)) # Allocate the temporary images gray = cv.CreateImage((image_width, image_height), 8, 1) #tuple as the first arg smallImage = cv.CreateImage((cv.Round( image_width / image_scale), cv.Round(image_height / image_scale)), 8, 1) (ok, img) = cam_img.read() #print 'gray is of ',type(gray) >>> gray is of <type 'cv2.cv.iplimage'> #print type(smallImage) >>> <type 'cv2.cv.iplimage'> #print type(image) >>> <type 'cv2.VideoCapture'> #print type(img) >>> <type 'numpy.ndarray'> #convert numpy.ndarray to iplimage ipl_img = cv2.cv.CreateImageHeader((img.shape[1], img.shape[0]), cv.IPL_DEPTH_8U, 3) cv2.cv.SetData(ipl_img, img.tostring(), img.dtype.itemsize * 3 * img.shape[1]) # Convert color input image to grayscale cv.CvtColor(ipl_img, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # => The function returns a list of tuples, (rect, neighbors) , where rect is a CvRect specifying the object’s extents and neighbors is a number of neighbors. # => CvRect cvRect(int x, int y, int width, int height) # If faces are found if faces: face = faces[0] self.faceX = face[0][0] self.faceY = face[0][1] for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(ipl_img, pt1, pt2, cv.RGB(0, 0, 255), 3, 8, 0) #face_region = cv.GetSubRect(ipl_img,(x,int(y + (h/4)),w,int(h/2))) cv.SetImageROI( ipl_img, (pt1[0], pt1[1], pt2[0] - pt1[0], int( (pt2[1] - pt1[1]) * 0.7))) eyes = cv.HaarDetectObjects(ipl_img, eyeCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, (15, 15)) if eyes: # For each eye found for eye in eyes: # Draw a rectangle around the eye cv.Rectangle( ipl_img, #image ( eye[0][0], #vertex pt1 eye[0][1]), ( eye[0][0] + eye[0][2], #vertex pt2 opposite to pt1 eye[0][1] + eye[0][3]), cv.RGB(255, 0, 0), 1, 4, 0) #color,thickness,lineType(8,4,cv.CV_AA),shift cv.ResetImageROI(ipl_img) return ipl_img
def parse_detail_page(self, page, link): print 3 doc_id = link.split('/')[ -1] #http://openlaw.cn/judgement/ea86414b0cac4075a3b88fcd9f8d4139 d = pq(page) #判断是否出现验证码 try: if u'请输入验证码' in d.text(): print u'等待输入验证码 > ' imgfoler = '/img/' basic = 'img' webimage = imgfoler + utility.get_unique_name() uniquename = basic + webimage self.driver.save_screenshot(uniquename + '_s.png') #截屏 _s.png captcha_image = self.driver.find_element_by_xpath( '//img[@id="kaptcha"]') loc = captcha_image.location loc['x'] = int(loc['x']) loc['y'] = int(loc['y']) image = cv.LoadImage(uniquename + '_s.png', True) out = cv.CreateImage((200, 50), image.depth, 3) cv.SetImageROI(image, (loc['x'], loc['y'], 200, 50)) cv.Resize(image, out) imgname = uniquename + '.jpg' cv.SaveImage(imgname, out) # 使用外部服务解码 result = captchaservice.getCaptcha(imgname) dictresult = json.loads(result) if dictresult.has_key('Error'): resultno = 1 raise Exception('service does not work well !') #endif code = dictresult['Result'] inputkey = self.driver.find_element_by_xpath( '//input[@class="search-field"]') inputkey.clear() inputkey.send_keys(code) time.sleep(2) searchbtn = self.driver.find_element_by_xpath( '//input[@type="submit"]') searchbtn.click() time.sleep(10) except: pass data = {} title = d('h2.entry-title').text() if '404' in d('title').text(): print ' [!] ERROR page, 404 not found, %s' % link return if not title: print ' [!] Empty page, resend %s' % link #如果页面为空,则将链接再发送到队列 self.channel.basic_publish( exchange='', routing_key='doc_queue', body=link, properties=pika.BasicProperties( delivery_mode=2, # 使消息持久化 )) time.sleep(.5) return #print title print 4 #提取结构化信息 侧边栏(sidebar) reason = trim_colon( d('aside#sidebar section').eq(0).find('li').filter( lambda i: u'案由' in pq(this).text()).text()) court = trim_colon( d('aside#sidebar section').eq(0).find('li').filter( lambda i: u'法院' in pq(this).text()).text()) doc_type = trim_colon( d('aside#sidebar section').eq(0).find('li').filter( lambda i: u'类型' in pq(this).text()).text()) status = trim_colon( d('aside#sidebar section').eq(0).find('li').filter( lambda i: u'程序' in pq(this).text()).text()) date = trim_colon(d('li.ht-kb-em-date').text()).strip() #strip() 去前后空格 regx = re.match(r'\d{4}-\d{2}-\d{2}', date) if not regx: date = '1970-01-01' case_id = trim_colon(d('li.ht-kb-em-category').text()) content = d('div#entry-cont').text().strip(u' 允许所有人 查看 该批注 \ 允许所有人 编辑 该批注 取消 保存 Annotate') # 人物侧边栏 persons = d('aside#sidebar section').eq(1) # 原告 accuser = filter_person(persons, [u'原告', u'审请人', u'上诉人', u'再审申请人']) # 被告 accused = filter_person(persons, [u'被告', u'被审请人', u'被上诉人']) # 审判长 chief_judge = filter_person(persons, [u'审判长']) # 律师 lawyers = filter_lawyers(persons) data['title'] = title data['title_search'] = title data['reason'] = reason data['court'] = court data['date'] = date data['doc_type'] = doc_type data['status'] = status data['content'] = content data['case_id'] = case_id data['lawyers'] = lawyers data['accuser'] = accuser data['accused'] = accused data['chief_judge'] = chief_judge data['url'] = link #导入elasticsearch #self.es.index(index=ES_INDEX, doc_type=ES_TYPE, id=doc_id, body=data) #print 'data',data #convertedDict = xmltodict.parse(data); realpath = link extraction = {} extraction['realpath'] = realpath extraction['data'] = data data1 = {} data1['extraction'] = extraction convertedXml = xmltodict.unparse(data1) # print "convertedXml=",convertedXml; try: folder = './result/' filename = folder + data['case_id'] + '.xml' f = open(filename, 'w') f.write(convertedXml) f.close() except: print 'error...'
#!/usr/bin/env python # -*- coding: utf-8 -*- import cv2.cv as cv import os img = cv.LoadImage("d:/flappybird/atlas.png", -1) #读取原图(这就是那张拼过的图片,我改了一下名字) mask = cv.LoadImage("d:/flappybird/atlas.png", 0) #读取mask width, height = cv.GetSize(img) img2 = cv.CreateImage((width, height), 8, 4) #创建一张背景透明的图片 cv.Copy(img, img2, mask) #copy过去 file = open("d:/flappybird/atlas.txt") #这是输入的文件 for line in file: strs = line.split(" ") fileName = strs[0] imgWidth = int(strs[1]) imgHeight = int(strs[2]) imgX = float(strs[3]) * width imgY = float(strs[4]) * height cv.SetImageROI(img2, (int(imgX), int(imgY), imgWidth, imgHeight)) #设置感兴趣的区域 cv.SaveImage("d:/flappybird/" + fileName + ".png", img2) #根据读取到的文件名保存到这个路径下
def get_next_link(self, page, link): """获取下一页链接""" d = pq(page) next_link = d('a.next.page-numbers').attr('href') if not next_link: # 如果不存在next_link,可能是最后一页 # 也可能是网站问题,默认重试获取这个链接3次 if u'请输入验证码' in d.text(): print u'等待输入验证码 >' cpt_tip = self.driver.find_element_by_xpath('//img[@id="kaptcha"]') imgfoler = '/img/' basic = 'img' webimage = imgfoler + utility.get_unique_name() uniquename = basic + webimage self.driver.save_screenshot(uniquename + '_s.png') #截屏 _s.png captcha_image = self.driver.find_element_by_xpath('//img[@id="kaptcha"]') loc = captcha_image.location loc['x']=int(loc['x']) loc['y']=int(loc['y']) image = cv.LoadImage(uniquename + '_s.png', True) out = cv.CreateImage((200,50), image.depth, 3) cv.SetImageROI(image,(loc['x'],loc['y'], 200, 50)) print '5' cv.Resize(image,out) imgname = uniquename + '.jpg' cv.SaveImage(imgname, out) # 使用外部服务解码 result = captchaservice.getCaptcha(imgname) dictresult = json.loads(result) if dictresult.has_key('Error') : resultno = 1 raise Exception('service does not work well !') #endif code = dictresult['Result'] inputkey = self.driver.find_element_by_xpath('//input[@class="search-field"]') inputkey.clear() inputkey.send_keys(code) time.sleep(10) searchbtn = self.driver.find_element_by_xpath('//input[@type="submit"]') searchbtn.click() time.sleep(1) #return 'err' # 如果没有next_link,则判断3次,如果有则继续,没有就说明是最后一页 for i in range(3): self.driver.get(link) # time.sleep(5) page = self.driver.find_element_by_xpath( '//*').get_attribute('outerHTML') d = pq(page) next_link = d('a.next.page-numbers').attr('href') if next_link: break # 再重新判断 if next_link: next_link = urlparse.urljoin(link, next_link) return next_link
def DetectRedEyes(image, faceCascade, smileCascade): min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 # Allocate the temporary images gray = cv.CreateImage((image.width, image.height), 8, 1) smallImage = cv.CreateImage((cv.Round( image.width / image_scale), cv.Round(image.height / image_scale)), 8, 1) # Convert color input image to grayscale cv.CvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: #print faces for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints #print "face" pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) # print pt1 # print pt2 #cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 1, 8, 0) #cv.PutText(image, "face", pt1, font, cv.RGB(255, 0, 0)) face_region = cv.GetSubRect(image, (x, int(y + (h / 4)), w, int(h / 2))) #split face #cv.Rectangle(image, (pt1[0],(pt1[1] + (abs(pt1[1]-pt2[1]) / 2 ))), pt2, cv.RGB(0,255,0), 1, 8, 0) #cv.PutText(image, "lower", (pt1[0],(pt1[1] + (abs(pt1[1]-pt2[1]) / 2 ))), font, cv.RGB(0, 255, 0)) cv.SetImageROI( image, (pt1[0], (pt1[1] + (abs(pt1[1] - pt2[1]) / 2)), pt2[0] - pt1[0], int((pt2[1] - (pt1[1] + (abs(pt1[1] - pt2[1]) / 2)))))) smiles = cv.HaarDetectObjects(image, smileCascade, cv.CreateMemStorage(0), 1.1, 5, 0, (15, 15)) if smiles: #print smiles for smile in smiles: cv.Rectangle( image, (smile[0][0], smile[0][1]), (smile[0][0] + smile[0][2], smile[0][1] + smile[0][3]), cv.RGB(0, 0, 255), 1, 8, 0) cv.PutText(image, "smile", (smile[0][0], smile[0][1]), font, cv.RGB(0, 0, 255)) cv.PutText(image, str(smile[1]), (smile[0][0], smile[0][1] + smile[0][3]), font, cv.RGB(0, 0, 255)) #print ((abs(smile[0][1] - smile[0][2]) / abs(pt1[0] - pt2[0])) * 100) global smileness smileness = smile[1] cv.ResetImageROI(image) #if smile[1] > 90: # mqttc.publish("smiles", "got smile", 1) # time.sleep(5) #eyes = cv.HaarDetectObjects(image, eyeCascade, #cv.CreateMemStorage(0), #haar_scale, min_neighbors, #haar_flags, (15,15)) #if eyes: # For each eye found #print eyes #for eye in eyes: # Draw a rectangle around the eye # cv.Rectangle(image, # (eye[0][0], # eye[0][1]), # (eye[0][0] + eye[0][2], # eye[0][1] + eye[0][3]), # cv.RGB(255, 0, 0), 1, 8, 0) cv.ResetImageROI(image) return image
for e in SignsList: imagesList[e] = cv.LoadImage("signs/" + e, cv.CV_LOAD_IMAGE_GRAYSCALE) #imagesList.append(cv.LoadImage("signs/"+e,cv.CV_LOAD_IMAGE_GRAYSCALE)) cv.NamedWindow("Input", cv.CV_WINDOW_AUTOSIZE) cv.NamedWindow("Gesture Space", cv.CV_WINDOW_AUTOSIZE) matchresult = 1 p_capWebcam = cv.CaptureFromCAM(0) while 1: p_imgOriginal = cv.QueryFrame(p_capWebcam) cv.Flip(p_imgOriginal, p_imgOriginal, 1) # capture from webcam p_gray = cv.CreateImage(cv.GetSize(p_imgOriginal), 8, 1) cv.CvtColor(p_imgOriginal, p_gray, cv.CV_BGR2GRAY) cv.SetImageROI(p_gray, (400, 200, 200, 200)) # Region setting of fixed interest cv.Threshold(p_gray, p_gray, 100, 255, cv.CV_THRESH_BINARY_INV) cv.Rectangle(p_imgOriginal, (400, 200), (600, 400), (255, 0, 0), 4) j = 0 for imageI in imagesList: # path of the image list and test each image with the ROI (region of interest) #image_to_test=cv.LoadImage("signs/"+image_path,cv.CV_LOAD_IMAGE_GRAYSCALE) matchresult = compare_2_formes(p_gray, imagesList[imageI]) #comparison #print("le match est "+str(matchresult)) if matchresult < 0.13 and matchresult != 0: sign_name = imageI.split('.')[0] print("letter :" + sign_name + ",with a matching of :" + str(matchresult)) cv.PutText(p_imgOriginal, sign_name, (5, 120), font, 255)
def DetectEyes(image, faceCascade, eyeCascade): min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 3 haar_flags = cv.CV_HAAR_DO_CANNY_PRUNING # Allocate the temporary images gray = cv.CreateImage((image.width, image.height), 8, 1) smallImage = cv.CreateImage((cv.Round( image.width / image_scale), cv.Round(image.height / image_scale)), 8, 1) # Convert color input image to grayscale cv.CvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) # If faces are found if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) face_region = cv.GetSubRect(image, (x, int(y + (h / 4)), w, int(h / 2))) cv.SetImageROI( image, (pt1[0], pt1[1], pt2[0] - pt1[0], int((pt2[1] - pt1[1]) * 0.7))) # If there are no faces found there's no reason to continue else: sys.exit("No faces were found") # NOTE: This returns the eye regions we're interested in eyes = cv.HaarDetectObjects(image, eyeCascade, cv.CreateMemStorage(0), 1.3, min_neighbors, haar_flags, (15, 15)) ## Draw rectangles around the eyes found ## if eyes: # For each eye found for eye in eyes: # Draw a rectangle around the eye cv.Rectangle(image, (eye[0][0], eye[0][1]), (eye[0][0] + eye[0][2], eye[0][1] + eye[0][3]), cv.RGB(255, 0, 0), 1, 8, 0) # The following is commented out because as we're debugging we don't # need to see the original image, just the regions of interest we have. #cv.ResetImageROI(image) return image
import cv2.cv as cv im = cv.LoadImage("../img/lena.jpg", 3) cv.SetImageROI(im, (50, 50, 150, 150)) cv.Zero(im) #cv.Set(im, cv.RGB(100, 100, 100)) cv.ResetImageROI(im) cv.ShowImage("Image", im) cv.WaitKey(0)
import cv2.cv as cv im = cv.LoadImage("meinv.jpg", cv.CV_8U) cv.SetImageROI(im, (1, 1, 30, 30)) histsize = 256 #Because we are working on grayscale pictures hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0, histsize]], 1) cv.CalcHist([im], hist) cv.NormalizeHist( hist, 1) # The factor rescale values by multiplying values by the factor _, max_value, _, _ = cv.GetMinMaxHistValue(hist) if max_value == 0: max_value = 1.0 cv.NormalizeHist(hist, 256 / max_value) cv.ResetImageROI(im) res = cv.CreateMat(im.height, im.width, cv.CV_8U) cv.CalcBackProject([im], res, hist) cv.Rectangle(im, (1, 1), (30, 30), (0, 0, 255), 2, cv.CV_FILLED) cv.ShowImage("Original Image", im) cv.ShowImage("BackProjected", res) cv.WaitKey(0)
def DetectRedEyes(image, faceCascade, smileCascade, eyeCascade): min_size = (20,20) image_scale = 2 haar_scale = 1.1 min_neighbors = 2 haar_flags = 0 # Allocate the temporary images gray = cv.CreateImage((image.width, image.height), 8, 1) smallImage = cv.CreateImage((cv.Round(image.width / image_scale),cv.Round (image.height / image_scale)), 8 ,1) # Convert color input image to grayscale cv.CvtColor(image, gray, cv.CV_BGR2GRAY) # Scale input image for faster processing cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR) # Equalize the histogram cv.EqualizeHist(smallImage, smallImage) # Detect the faces faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) global norm # If faces are found if faces: #print faces ratio = 1. for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints #print "face" if h!=0: ratio = h/norm pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) # print pt1 # print pt2 #cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 1, 8, 0) #cv.PutText(image, "face"+str(h), pt1, font, cv.RGB(255, 0, 0)) face_region = cv.GetSubRect(image,(x,int(y + (h/4)),w,int(h/2))) #split face #cv.Rectangle(image, (pt1[0],(pt1[1] + (abs(pt1[1]-pt2[1]) / 2 ))), pt2, cv.RGB(0,255,0), 1, 8, 0) #cv.PutText(image, "lower", (pt1[0],(pt1[1] + (abs(pt1[1]-pt2[1]) / 2 ))), font, cv.RGB(0, 255, 0)) cv.SetImageROI(image, (pt1[0], (pt1[1] + int(abs(pt1[1]-pt2[1]) * 0.625 )), pt2[0] - pt1[0], int((pt2[1] - (pt1[1] + int(abs(pt1[1]-pt2[1]) * 0.625 )))))) smiles = cv.HaarDetectObjects(image, smileCascade, cv.CreateMemStorage(0), 1.1, 5, 0, (15,15)) if smiles: #print smiles for smile in smiles: cv.Rectangle(image, (smile[0][0],smile[0][1]), (smile[0][0] + smile[0][2], smile[0][1] + smile[0][3]), cv.RGB(0, 0, 255), 1, 8, 0) sizer = (smile[0][2]/ratio+smile[0][3]/ratio)#+(smile[1]/ratio)) #sizer = math.trunc(sizer) #cv.PutText(image, "smile", (smile[0][0],smile[0][1]), font, cv.RGB(0, 0, 255)) cv.PutText(image,str(math.trunc(sizer**2)), (smile[0][0], smile[0][1] + smile[0][3] + 10), font, cv.RGB(0, 0, 255)) #print ((abs(smile[0][1] - smile[0][2]) / abs(pt1[0] - pt2[0])) * 100) global smileneighbour smileneighbour = sizer**2*2 cv.ResetImageROI(image) ############################################################################# ############################################################################# cv.SetImageROI(image, (pt1[0], pt1[1], int(pt2[0]-pt1[0]), int(pt2[1] - pt1[1])) ) eyes = cv.HaarDetectObjects(image, eyeCascade,cv.CreateMemStorage(0),haar_scale, 5,haar_flags, (15,15)) if eyes: # For each eye found iii = 0 #print eyes for eye in eyes: # Draw a rectangle around the eye cv.Rectangle(image,(eye[0][0],eye[0][1]),(eye[0][0] + eye[0][2],eye[0][1] + eye[0][3]), cv.RGB(0, 0, 255), 1, 8, 0) a = math.trunc(float(eye[1])/ratio) cv.PutText(image,str(a), (eye[0][0], eye[0][1] + eye[0][3]), font, cv.RGB(0, 0, 255)) global eyetot eyetot += float(eye[1]*eye[1])/ratio iii+=1 if iii==2: iii = 0 break cv.ResetImageROI(image) cv.ResetImageROI(image) return image
if count > 0: y_flag = 1 else: y_flag = 0 if pre_y_flag != y_flag: h_pos.append(y) match_img = [] tmp_mg = [] for i in range(0, leng / 2): cv2.waitKey(3) cv2.rectangle(image, (w_pos[i], h_pos[i]), (w_pos[i + 1], h_pos[i + 1]), (255, 255, 0)) imgIP = cv.GetImage(cv.fromarray(image)) cv.SetImageROI(imgIP, (w_pos[2 * i], h_pos[2 * i], w_pos[2 * i + 1] - w_pos[2 * i], h_pos[2 * i + 1] - h_pos[2 * i])) img_cache = cv.CreateImage( (w_pos[2 * i + 1] - w_pos[2 * i], h_pos[2 * i + 1] - h_pos[2 * i]), cv.IPL_DEPTH_8U, 3) cv.Copy(imgIP, img_cache) match_img.append(img_cache) # cv.SaveImage('ss'+bytes(i)+'.jpg',match_img[i]) cv.ResetImageROI(imgIP) # cv.ShowImage('window'+bytes(i),match_img[i]) #aa=image.copy((range(10,20),range(20,100))) #cv2.imshow("hello",aa) #cv2.imshow("img",image) print w_pos print h_pos cv2.waitKey(0)