我正在尝试从托管网址获取网站名称(例如“ www.google.com”-> google,“ facebook.com”-> facebook) 目前,我有这个简单的功能:
import numpy as np
import time
import cv2
INPUT_FILE= ('Outputs/original.jpg')
OUTPUT_FILE=('Outputs/_yolo.jpg')
LABELS_FILE='data/classes.names'
CONFIG_FILE='data/yolov3.cfg'
WEIGHTS_FILE='data/lapi.weights'
CONFIDENCE_THRESHOLD=0.3
LABELS = open(LABELS_FILE).read().strip().split("\n")
np.random.seed(4)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3),
dtype="uint8")
net = cv2.dnn.readNetFromDarknet(CONFIG_FILE, WEIGHTS_FILE)
image = cv2.imread(INPUT_FILE)
(H, W) = image.shape[:2]
# determine only the *output* layer names that we need from YOLO
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),
swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(ln)
end = time.time()
print("[INFO] YOLO took {:.6f} seconds".format(end - start))
# initialize our lists of detected bounding boxes, confidences, and
# class IDs, respectively
boxes = []
confidences = []
classIDs = []
# loop over each of the layer outputs
for output in layerOutputs:
# loop over each of the detections
for detection in output:
# extract the class ID and confidence (i.e., probability) of
# the current object detection
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
# filter out weak predictions by ensuring the detected
# probability is greater than the minimum probability
if confidence > CONFIDENCE_THRESHOLD:
# scale the bounding box coordinates back relative to the
# size of the image, keeping in mind that YOLO actually
# returns the center (x, y)-coordinates of the bounding
# box followed by the boxes' width and height
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
# use the center (x, y)-coordinates to derive the top and
# and left corner of the bounding box
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
# update our list of bounding box coordinates, confidences,
# and class IDs
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
# apply non-maxima suppression to suppress weak, overlapping bounding
# boxes
idxs = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE_THRESHOLD,
CONFIDENCE_THRESHOLD)
# ensure at least one detection exists
if len(idxs) > 0:
# loop over the indexes we are keeping
for i in idxs.flatten():
# extract the bounding box coordinates
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
color = [int(c) for c in COLORS[classIDs[i]]]
cv2.rectangle(image, (x, y), (x + w, y + h), color, 20)
text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.5, color, 20)
# save the output image
ratio = 490.0 / image.shape[1]
dim = (490, int(image.shape[0] * ratio))
resized_img = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
cv2.imwrite(('Outputs/_yolo.jpg'), resized_img)
cropped_img = cv2.imread('Outputs/original.jpg')
cropped_img = cropped_img[y:y+h, x:x+w]
ratio = 490.0 / cropped_img.shape[1]
dim = (490, int(cropped_img.shape[0] * ratio))
resized_img = cv2.resize(cropped_img, dim, interpolation = cv2.INTER_AREA)
cv2.imwrite(('Outputs/license.jpg'), resized_img)
对于许多网站来说,这并不算不好,但是当然还有很多其他网站并不能正常工作,例如:“ medium.com”只是返回com
我也尝试了数“。”和其他方法,但同样,它对某些人有用,对其他人无效。
提取这样的东西有什么约定吗? 如果没有,如何提取网站名称,只有正则表达式是唯一的选择?
答案 0 :(得分:0)
也许尝试像这样将您的链接放在URL
类中
URL url = new URL(address);
然后您可以使用
String host = url.getHost();
编辑:这个问题看起来像duplicate ...