python map示例代码
时间: 2023-11-19 15:50:00 浏览: 28
map函数是Python内置的一个函数,它接受一个函数和一个可迭代对象作为参数,然后将该函数依次作用于可迭代对象的每个元素,并返回一个新的可迭代对象。
下面是一个示例代码,展示了如何使用map函数对列表中的每个元素进行平方操作:
```python
def square(x):
return x**2
nums = [1, 2, 3, 4, 5]
squared_nums = list(map(square, nums))
print(squared_nums)
```
输出结果为:[1, 4, 9, 16, 25]
相关问题
python目标检测代码示例
当然,下面是一个简单的 Python 目标检测代码示例,使用了 OpenCV 和 TensorFlow 的目标检测 API:
```python
import cv2
import numpy as np
import tensorflow as tf
# 加载模型
model_path = 'path_to_model/frozen_inference_graph.pb'
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(model_path, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
# 加载标签
label_map = 'path_to_labels/label_map.pbtxt'
categories = []
with open(label_map, 'r') as f:
lines = f.readlines()
for line in lines:
if 'name' in line:
category = line.split(':')[-1].strip().replace("'", "")
categories.append(category)
# 进行目标检测
def detect_objects(image):
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
image_np_expanded = np.expand_dims(image, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# 显示检测结果
for i in range(int(num_detections[0])):
if scores[0][i] > 0.5:
class_index = int(classes[0][i])
class_name = categories[class_index - 1]
ymin, xmin, ymax, xmax = boxes[0][i]
xmin = int(xmin * image.shape[1])
xmax = int(xmax * image.shape[1])
ymin = int(ymin * image.shape[0])
ymax = int(ymax * image.shape[0])
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
cv2.putText(image, class_name, (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
return image
# 加载图像
image_path = 'path_to_image/image.jpg'
image = cv2.imread(image_path)
# 进行目标检测并显示结果
output_image = detect_objects(image)
cv2.imshow("Output", output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这个示例代码使用了一个已经训练好的目标检测模型(frozen_inference_graph.pb)和对应的标签文件(label_map.pbtxt)。你需要将模型路径和标签路径替换为你自己的路径,并提供一张待检测的图像(image.jpg)。
注意:这个示例代码仅仅是一个简单的演示,如果你想要在实际项目中进行目标检测,可能需要更复杂的模型和数据预处理。
python百度街景数据爬取示例代码
当然,我可以给你提供一个Python百度街景数据爬取的示例代码。以下是一个简单的示例:
```python
import requests
import os
def download_street_view_images(location, output_dir):
url = f"http://api.map.baidu.com/panorama/v2?ak=your_api_key&width=512&height=256&location={location}&fov=360"
response = requests.get(url)
data = response.json()
if data["status"] == 0:
for i, street_view in enumerate(data["links"]):
image_url = street_view["link"]
image_name = f"{location}_{i}.jpg"
image_path = os.path.join(output_dir, image_name)
response = requests.get(image_url)
with open(image_path, "wb") as f:
f.write(response.content)
print(f"Downloaded image: {image_name}")
else:
print("Failed to retrieve street view data.")
# 示例用法
location = "39.915,116.404" # 经纬度坐标,这里以北京天安门为例
output_dir = "street_view_images" # 图片保存目录
download_street_view_images(location, output_dir)
```
请注意,上述示例代码中的`your_api_key`需要替换为你自己的百度地图API密钥。此外,你还需要安装`requests`库来发送HTTP请求。
这个示例代码通过调用百度地图API获取指定位置的街景数据,并将街景图片保存到指定目录中。你可以根据自己的需求进行修改和扩展。