16 图片

一,chooseImage

从本地相册选择图片或使用相机拍照

参数类型必填说明
countNumber最多可以选择的图片张数,默认9
sizeTypeStringArrayoriginal 原图,compressed 压缩图,默认二者都有
sourceTypeStringArrayalbum 从相册选图,camera 使用相机,默认二者都有
successFunction成功则返回图片的本地文件路径列表 tempFilePaths
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

注:文件的临时路径,在小程序本次启动期间可以正常使用,如需持久保存,需在主动调用 wx.saveFile,在小程序下次启动时才能访问得到。
示例代码:

wx.chooseImage({
  count: 1, // 默认9
  sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  success: function (res) {
    // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
    var tempFilePaths = res.tempFilePaths
  }
})

二,previewImage

预览图片

参数类型必填说明
currentString当前显示图片的链接,不填则默认为 urls 的第一张
urlsStringArray需要预览的图片链接列表
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

wx.previewImage({
  current: '', // 当前显示图片的http链接
  urls: [] // 需要预览的图片http链接列表
})

三,getImageInfo

获取图片信息

参数类型必填说明
srcString图片的路径,可以是相对路径,临时文件路径,存储文件路径,网络图片路径
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数类型说明
widthNumber图片宽度,单位px
heightNumber图片高度,单位px
pathString返回图片的本地路径

示例代码:

wx.getImageInfo({
  src: 'images/a.jpg',
  success: function (res) {
    console.log(res.width)
    console.log(res.height)
  }
})

wx.chooseImage({
  success: function (res) {
    wx.getImageInfo({
      src: res.tempFilePaths[0],
      success: function (res) {
        console.log(res.width)
        console.log(res.height)
      }
    })
  }
})
### VGG16用于图片特征提取的方法 在PyTorch中,可以使用预训练的VGG16模型来提取图像特征。具体来说,可以通过截取VGG16网络的最后一层卷积层(`conv5_3`),并将其作为特征提取器[^1]。 以下是基于PyTorch实现的一个简单代码示例: ```python import torch from torchvision import models from torch import nn # 加载预训练的VGG16模型 vgg16 = models.vgg16(pretrained=True) # 定义一个新的模型,只保留到最后一层卷积层的部分 class FeatureExtractor(nn.Module): def __init__(self, vgg_model): super(FeatureExtractor, self).__init__() self.features = list(vgg_model.features.children())[:30] # 提取前30层 (直到 conv5_3) self.feature_extractor = nn.Sequential(*self.features) def forward(self, x): return self.feature_extractor(x) # 创建特征提取器实例 feature_extractor = FeatureExtractor(vgg16).to(device) # 将模型移动到指定设备 # 测试输入数据 transform = transforms.Compose([ transforms.Resize((224, 224)), # 调整大小至224x224 transforms.ToTensor(), # 转换为张量 ]) image_path = 'example.jpg' # 替换为你自己的图片路径 img = PIL.Image.open(image_path) img_tensor = transform(img).unsqueeze(0) # 添加批次维度 img_tensor = img_tensor.to(device) # 移动到指定设备 # 提取特征 with torch.no_grad(): # 关闭梯度计算以节省内存 features = feature_extractor(img_tensor) print(f"Extracted Features Shape: {features.shape}") # 输出特征形状 ``` 上述代码定义了一个新的类 `FeatureExtractor` 来裁剪原始的VGG16模型,仅保留其卷积部分直至最后的卷积层。通过这种方式,可以从给定的图像中提取高级语义特征。 对于TensorFlow中的实现方式,则有所不同。以下是一个简单的TensorFlow/Keras版本: ```python import tensorflow as tf from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input from tensorflow.keras.preprocessing.image import load_img, img_to_array # 加载预训练的VGG16模型,并移除顶部全连接层 base_model = VGG16(weights='imagenet', include_top=False) def extract_features(file_name): # 加载和预处理图像 image = load_img(file_name, target_size=(224, 224)) image = img_to_array(image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) # 提取特征 features = base_model.predict(image) return features.flatten() file_name = 'example.jpg' # 替换为你自己的图片路径 features = extract_features(file_name) print(f"Features shape: {features.shape}") ``` 此代码加载了不带顶层分类器的VGG16模型,并对单个图像进行了特征提取操作[^2]。 #### 设备选择 无论是在PyTorch还是TensorFlow中运行该过程,都建议优先考虑GPU加速。如果当前环境支持CUDA,则会自动启用GPU运算;否则,默认回退到CPU模式[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值