code: def raster2shp(rasterfile, vectorshp, layername=None, fieldname=None,
                     band_num=1, mask='default'):
          
          FileClass.remove_files(vectorshp)
          FileClass.check_file_exists(rasterfile)
          # this allows GDAL to throw Python Exceptions
          gdal.UseExceptions()
          src_ds = gdal.Open(rasterfile)
          if src_ds is None:
              print('Unable to open %s' % rasterfile)
              sys.exit(1)
          try:
              srcband = src_ds.GetRasterBand(band_num)
          except RuntimeError as e:
              # for example, try GetRasterBand(10)
              print('Band ( %i ) not found, %s' % (band_num, e))
              sys.exit(1)
          if mask == 'default':
              maskband = srcband.GetMaskBand()
          elif mask is None or mask.upper() == 'NONE':
              maskband = None
          else:
              mask_ds = gdal.Open(mask)
              maskband = mask_ds.GetRasterBand(1)
          #  create output datasource
          if layername is None:
              layername = FileClass.get_core_name_without_suffix(rasterfile)
          drv = ogr_GetDriverByName(str('ESRI Shapefile'))
          dst_ds = drv.CreateDataSource(vectorshp)
          srs = None
          if src_ds.GetProjection() != '':
              srs = osr_SpatialReference()
              srs.ImportFromWkt(src_ds.GetProjection())
          dst_layer = dst_ds.CreateLayer(str(layername), srs=srs)
          if fieldname is None:
              fieldname = layername.upper()
          fd = ogr_FieldDefn(str(fieldname), OFTInteger)
          dst_layer.CreateField(fd)
          dst_field = 0
          result = gdal.Polygonize(srcband, maskband, dst_layer, dst_field,
                                   ['8CONNECTED=8'], callback=None)
          return result
docs: Convert raster to ESRI shapefile